I'm relativley new to python I have a excel file where i can read,Column A "url" and Column B "name".
In the future the columns will have no "column name" so i need it to read from Column A directly and column B and start iterating from cell 1.
I tried using index_col(0) but can't really seem to get the hang of it. This is a simple download image script.
import requests
import pandas as pd
df = pd.read_excel(r'C:\Users\exdata1.xlsx')
for index, row in df.iterrows():
url = row['url']
file_name = url.split('/')
r = requests.get(url)
file_name=(row['name'] ".jpeg")
if r.status_code == 200:
with open(file_name, "wb") as f:
f.write(r.content)
print (file_name)
I tried this below without any good result.
url = row['index_col(0)'] #0 for excel column "A"
file_name=(row['index_col(1)'] ".jpeg") #1 for excel Column "B"
Apreciate any support!
CodePudding user response:
You can set header=None
as an argument of pandas.read_excel
and give names to your columns.
Try this :
import requests
import pandas as pd
df = pd.read_excel(r'C:\Users\exdata1.xlsx', header=None, names=['url', 'name'])
for index, row in df.iterrows():
url = row['url']
file_name = url.split('/')
r = requests.get(url)
file_name=(row['name'] '.jpeg')
if r.status_code == 200:
with open(file_name, 'wb') as f:
f.write(r.content)
print(file_name)
CodePudding user response:
If your files had no columns name pandas assign values to each column such as Unnamed: 0
, you can check that py printing df.info
or df.head()
you can assign columns names when reading from your file so you df always has columns name:
df.rename( columns={"Unnamed: 0" :'url', Unnamed: 0: 'name'}, inplace=True )
then you are good to go.