在 pandas 中使用BytesIO read_csv [英] Using BytesIO in pandas read_csv

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

问题描述

我从URL获取一个csv文件,并且试图通过Python3使用pandas.read_csv读取它,但是由于某种原因,我得到了parserError.这是代码:

I am getting a csv file from a url and I am trying to read it using pandas.read_csv with Python3, but I am getting a parserError for some reason. Here's the code:

csvurl = requests.get("https://github.com/cs109/2014_data/blob/master/countries.csv")
csvb = BytesIO(csvurl.content)
countries = pd.read_csv(csvb)

并且出现以下错误:

ParserError:标记数据时出错.C错误:第114行中应有1个字段,看到了3

ParserError: Error tokenizing data. C error: Expected 1 fields in line 114, saw 3

但是第114行与其他行相同.错误与我尝试使用Bytes对象读取csv有关吗?

But line 114 is just the same as the other lines. Is the error related to me trying to read csv using Bytes object?

推荐答案

仅使用原始数据网址:

url = 'https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv'
countries = pd.read_csv(url)
print (countries)

                              Country         Region
0                             Algeria         AFRICA
1                              Angola         AFRICA
2                               Benin         AFRICA
3                            Botswana         AFRICA
4                             Burkina         AFRICA
5                             Burundi         AFRICA
6                            Cameroon         AFRICA
7                          Cape Verde         AFRICA
8            Central African Republic         AFRICA
9                                Chad         AFRICA

这篇关于在 pandas 中使用BytesIO read_csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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