Pandas read_csv来自url [英] Pandas read_csv from url

查看:547
本文介绍了Pandas read_csv来自url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 3.4与IPython,并有以下代码。我无法从给定的网址读取csv文件:

I am using Python 3.4 with IPython and have the following code. I'm unable to read a csv-file from the given URL:

import pandas as pd
import requests

url="https://github.com/cs109/2014_data/blob/master/countries.csv"
s=requests.get(url).content
c=pd.read_csv(s)

我有以下错误


预期的文件路径名或类似文件的对象,获取类型

"Expected file path name or file-like object, got type"

解决方案

推荐答案

正如错误所示, pandas.read_csv

如果要从字符串中读取csv,可以使用 io.StringIO (Python 3.x)或 StringIO.StringIO ( Python 2.x)

If you want to read the csv from a string, you can use io.StringIO (Python 3.x) or StringIO.StringIO (Python 2.x) .

此外,对于网址 - https://github.com/cs109/2014_data/blob/master/countries.csv - 您正在收回 html 响应,而不是raw csv,你应该使用github页面中的 Raw 链接给出的url来获取原始csv响应,即 - https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv

Also, for the URL - https://github.com/cs109/2014_data/blob/master/countries.csv - you are getting back html response , not raw csv, you should use the url given by the Raw link in the github page for getting raw csv response , which is - https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv

示例 -

import pandas as pd
import io
import requests
url="https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
s=requests.get(url).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))

这篇关于Pandas read_csv来自url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆