'Iterating through csv and passing into request one by one
I am trying to make a program that iterates over a CSV file that only has one column and trying to pass it on as a request one-by-one, as the API can only handle one at a time.
This is the script so far:
import requests,csv
with open(r'C:/Users/Desktop/123.csv') as x:
reader = csv.reader(x)
for row in reader:
print (row[0])
r = requests.post("xxxyyyy.com",
data={
"ID": "xxx",
},
headers={
"Authorization" : "xxx",
"Content-type": "application/x-www-form-urlencoded",
"Accept": "application/json",
})
r_dict = r.json()
print(r_dict)
My problem is I do not know what to use to iterate over the CSV file and how to pass the string on as a JSON and then send it.
Solution 1:[1]
You are almost there, just pass row[0] to the request's payload instead of printing it:
import csv
import requests
with open(r'C:/Users/Desktop/123.csv') as x:
reader = csv.reader(x)
for row in reader:
r = requests.post("xxxyyyy.com",
data={
"ID": row[0],
},
headers={
"Authorization": "xxx",
"Content-type": "application/x-www-form-urlencoded",
"Accept": "application/json",
})
r_dict = r.json()
print(r_dict)
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 | Gabio |
