Python 使用 Pandas 将文本数据从请求对象转换为数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39213597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:59:56  来源:igfitidea点击:

Convert text data from requests object to dataframe with pandas

pythoncsvpandasdataframepython-requests

提问by sparrow

Using requests I am creating an object which is in .csv format. How can I then write that object to a DataFrame with pandas?

使用请求我正在创建一个 .csv 格式的对象。然后我怎样才能将该对象写入带有熊猫的 DataFrame 中?

To get the requests object in text format:

以文本格式获取请求对象:

import requests
import pandas as pd
url = r'http://test.url' 
r = requests.get(url)
r.text  #this will return the data as text in csv format

I tried (doesn't work):

我试过(不起作用):

pd.read_csv(r.text)
pd.DataFrame.from_csv(r.text)

回答by Merlin

Try this

尝试这个

import requests
import pandas as pd
import io

urlData = requests.get(url).content
rawData = pd.read_csv(io.StringIO(urlData.decode('utf-8')))

回答by jezrael

I think you can use read_csvwith url:

我想你可以用read_csvurl

pd.read_csv(url)

filepath_or_buffer: str, pathlib.Path, py._path.local.LocalPath or any object with a read() method (such as a file handle or StringIO)

The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.csv

filepath_or_buffer: str, pathlib.Path, py._path.local.LocalPath 或任何具有 read() 方法的对象(例如文件句柄或 StringIO)

该字符串可以是一个 URL。有效的 URL 方案包括 http、ftp、s3 和文件。对于文件 URL,需要一个主机。例如,本地文件可以是 file://localhost/path/to/table.csv

import pandas as pd
import io
import requests

url = r'http://...' 
r = requests.get(url)  
df = pd.read_csv(io.StringIO(r))


If it doesnt work, try update last line:

如果它不起作用,请尝试更新最后一行:

import pandas as pd
import io
import requests

url = r'http://...' 
r = requests.get(url)  
df = pd.read_csv(io.StringIO(r.text))

回答by rkoots

if the url has no authentication then you can directly use read_csv(url)

如果 url 没有身份验证,那么您可以直接使用 read_csv(url)

if you have authentication you can use request to get it un-pickel and print the csv and make sure the result is CSV and use panda.

如果您有身份验证,您可以使用请求将其取消pickel并打印csv并确保结果为CSV并使用panda。

You can directly use importing import csv

可以直接使用importing import csv

回答by Bento

Using "read_csv with url" worked:

使用“read_csv with url”有效:

import requests, csv
import pandas as pd
url = 'https://arte.folha.uol.com.br/ciencia/2020//csv/mundo/dados-bra.csv'
corona_bra = pd.read_csv(url)
print(corona_bra.head())