'i want to make urls
1.Link is "https://www.xyz.{country}/dp/{asin}" 2.I have to pick two things from csv file which country and asin. CSV file contains :
Asin Country
0 1011 de
1 1022 fr
2 1033 de
My question : How can I extract asin and country one by one and make urls like
https://www.xyz.{country}/dp/{asin} --> https://www.xyz.de/dp/1011
https://www.xyz.{country}/dp/{asin} --> https://www.xyz.fr/dp/1022
Solution 1:[1]
try this :
lst_url=[]
for index,row in df.iterrows():
country=df.loc[index,'Country']
asin=df.loc[index,'Asin']
url=f"https://www.xyz.{country}/dp/{asin}"
lst_url.append(url)
lst_url will contain all the url you want
Solution 2:[2]
Filter columns by list ['Country','Asin'], convert to numpy and in for get scalars passed to f-string:
df = pd.read_csv(file)
for country, asin in df[['Country','Asin']].to_numpy():
url=f"https://www.xyz.{country}/dp/{asin}"
print (url)
https://www.xyz.de/dp/1011
https://www.xyz.fr/dp/1022
https://www.xyz.de/dp/1033
For list use:
L = [f"https://www.xyz.{country}/dp/{asin}"
for country, asin in df[['Country','Asin']].to_numpy()]
Solution 3:[3]
with open(csv_file, 'r') as f:
datas = f.readlines()
for data in datas[1:]:
asin, country = data.split()
print(f'https://www.xyz.{country}/dp/{asin}')
result:
https://www.xyz.de/dp/1011
https://www.xyz.fr/dp/1022
https://www.xyz.de/dp/1033
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DataSciRookie |
| Solution 2 | jezrael |
| Solution 3 | linupy chiang |
