简体   繁体   English

直接从 Pandas 数据框 (pandas_to.csv) 发布 csv 文件

[英]post csv file directly from pandas data frame (pandas_to.csv)

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.我正在寻找一种直接从 Pandas 数据框中发布 csv 文件的方法,而无需将其保存在本地文件中,然后再发布。

In my case, the URL is a service listening to my request that requires a scv在我的例子中,URL 是一个服务,它监听我的请求,它需要一个 scv

Is there a way to have a url as an argument into a df.to_csv(url) that will post it directly?有没有办法将 url 作为参数放入 df.to_csv(url) 中,直接发布它?

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]>

@Anton vBR thanks! @Anton vBR 谢谢!

This turned out to work:结果证明这有效:

slider_output = agg_data.to_csv()
        files = {'file': ('agg_data.csv', slider_output)}
        requests.post('some_url', files=files)

Thanks @AntonvBR.谢谢@AntonvBR。 Below code worked for me with python 3.7.3下面的代码适用于 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM