If I wanted to push a csv file to an endpoint in Python, what is the correct way to do this?
with open("foo.csv") as f:
endpoint = 'some-url'
headers = {}
r = requests.post(endpoint, ...., headers = headers)
What would be the next steps?
If I wanted to push a csv file to an endpoint in Python, what is the correct way to do this?
with open("foo.csv") as f:
endpoint = 'some-url'
headers = {}
r = requests.post(endpoint, ...., headers = headers)
What would be the next steps?
it probably depends on the receiving end but a list of lists is a json object, so directly reading the file using csv
module could work.
import csv
with open("foo.csv") as f:
endpoint = 'some-url'
cr = csv.reader(f) # delimiter="," by default
r = requests.post(endpoint, json = list(cr))
or if the csv file has a title, pass a list of dicts
import csv
with open("foo.csv") as f:
endpoint = 'some-url'
cr = csv.DictReader(f) # delimiter="," by default
r = requests.post(endpoint, json = list(cr))
you can directly push your CSV file since requests
makes it simple to upload multipart encoded files
with open("foo.csv", "rb") as f:
file_data = {"file": f}
endpoint = "some-url"
response = requests.post(endpoint, files=file_data)