2

I'm looking for a way to post a csv file directly from a pandas data frame without the need of saving it in a local file, and then posting it.

In my case, the URL is a service listening to my request that requires a scv

Is there a way to have a url as an argument into a df.to_csv(url) that will post it directly?

I tried the following: (got bad response )

import requests
agg_data=pd.read_csv('some_csv.csv')
r = requests.post('http://crystal-ball-some_url', data ={'agg_data':agg_data.to_csv()})
print (r)
<Response [400]>
3
  • 4
    Post it where? HTTP post? Csv is a string already so just send it? .post(df.to_csv(index=False)) Commented Jun 6, 2018 at 11:06
  • @AntonvBR thanks for the help, see my edit Commented Jun 7, 2018 at 6:24
  • did you try to add the target URL string pd.to_csv(URL) instead of a filename, as if you would do when saving it to local file Commented Jun 7, 2018 at 6:39

2 Answers 2

3

@Anton vBR thanks!

This turned out to work:

slider_output = agg_data.to_csv()
        files = {'file': ('agg_data.csv', slider_output)}
        requests.post('some_url', files=files)
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks @AntonvBR. Below code worked for me with python 3.7.3

import requests
files = {('files', ('aug_template.csv', aug_annotation_df.to_csv()))}
response = requests.post(url=UPLOAD_URL, params=params, files=files, data=body)

1 Comment

Welcome to SO! When answering to an old question (this one is over 2 years old) that already has an accepted answer (that is the case here) make sure your answer adds something substantially new. Don't answer if not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.