Read ZipFile from URL into StringIO and parse with panda.read_csv

别说谁变了你拦得住时间么 提交于 2020-12-06 06:59:12

问题


I'm trying to read ZipFile data from a URL and via StringIO parse the data inside the ZipFile as csv using pandas.read_csv

r = req.get("http://seanlahman.com/files/database/lahman-csv_2014-02-14.zip").content
file = ZipFile(StringIO(r))
salaries_csv = file.open("Salaries.csv")
salaries = pd.read_csv(salaries_csv)

The last line gave me an error:

CParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.

However if i try using

salaries = pd.read_csv(file.open("Salaries.csv"))

it works.

So I was wondering what am I missing out here.

file.open should return a ZipExtFile object and since read_csv takes only string or file handle / StringIO input, why is the last line working then?


回答1:


I think something is wrong with the way you read the data, it works for me using urllib2.

from zipfile import ZipFile
from StringIO import StringIO
import urllib2

r = urllib2.urlopen("http://seanlahman.com/files/database/lahman-csv_2014-02-14.zip").read()
file = ZipFile(StringIO(r))
salaries_csv = file.open("Salaries.csv")
salaries = pd.read_csv(salaries_csv)
       yearID teamID lgID   playerID    salary
0        1985    BAL   AL  murraed02   1472819
1        1985    BAL   AL   lynnfr01   1090000
2        1985    BAL   AL  ripkeca01    800000
3        1985    BAL   AL   lacyle01    725000
4        1985    BAL   AL  flanami01    641667
5        1985    BAL   AL  boddimi01    625000
6        1985    BAL   AL  stewasa01    581250
7        1985    BAL   AL  martide01    560000


来源:https://stackoverflow.com/questions/31967635/read-zipfile-from-url-into-stringio-and-parse-with-panda-read-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!