我正在尝试使用pandas.read_csv
从IPython笔记本中读取URL中的bz2压缩CSV文件(在本例中为GitHub原始URL),但是我收到以下错误:
Python cannot read bz2 from open file handle
。
我做了一些研究,似乎无法弄清楚出了什么问题。我已经尝试手动解压缩bz2文件,我知道它们没有损坏或者其他方面都不好,而且我也知道URL格式正确 - 如果我将它们输入浏览器,它会正确下载文件。
这是我的IPython笔记本中的代码:
import pandas, bz2, urllib2
datafiles = {}
for filename in csvData['Filename']:
csvUrl = 'https://raw.github.com/hawkw/traverse/master/data/' + filename
try:
datafiles[filename] = pandas.read_csv(csvUrl, compression='bz2')
print (datafiles[filename])
except Exception as e:
print("Caught error \"{error}\" at {url}".format(error=e, url=csvUrl))
输出:
Caught error "Python cannot read bz2 from open file handle" at https://raw.github.com/hawkw/traverse/master/data/-Users-hawk-_2014-02-05.csv.bz2
Caught error "Python cannot read bz2 from open file handle" at https://raw.github.com/hawkw/traverse/master/data/-Users-Owner_2014-02-05.csv.bz2
Caught error "Python cannot read bz2 from open file handle" at https://raw.github.com/hawkw/traverse/master/data/-Users-will_2014-02-05.csv.bz2
Caught error "Python cannot read bz2 from open file handle" at https://raw.github.com/hawkw/traverse/master/data/-Users-hawk_2014-02-06.csv.bz2
Caught error "Python cannot read bz2 from open file handle" at https://raw.github.com/hawkw/traverse/master/data/-Users-hawk-Documents_2014-02-06.csv.bz2
Caught error "Python cannot read bz2 from open file handle" at https://raw.github.com/hawkw/traverse/master/data/-home-w-weismanm_2014-02-05.csv.bz2
Caught error "Python cannot read bz2 from open file handle" at https://raw.github.com/hawkw/traverse/master/data/-home-w-weismanm_2014-02-06.csv.bz2
有人知道我做错了吗?
编辑:我尝试使用urllib2
像这样打开文件,正如@edchum建议的那样:
datafiles = {}
for filename in csvData['Filename']:
url = 'https://raw.github.com/hawkw/traverse/master/data/' + filename
try:
response = urllib2.urlopen(url)
except HTTPError as e:
print ("Caught HTTPError", e)
else:
try:
datafiles[filename] = pandas.read_csv(response, compression='bz2')
print (datafiles[filename])
except Exception as e:
print("Caught error \"{0}\" at {1}".format(e,url))
但它仍然无效,失败并出现同样的错误。作为旁注,pandas.read_csv()
表示它可以从文档中的URL打开文件。
答案 0 :(得分:4)
如果您出于某种原因绝对无法简单地下载文件,并且必须继续从远程源(非常邻居)中提取文件,那么如果需要,您可以在内存中完成所有操作:
>>> import pandas as pd
>>> import io
>>> import urllib2
>>> import bz2
>>>
>>> url = "https://github.com/hawkw/traverse/blob/master/data/-Users-hawk_2014-02-06.csv.bz2?raw=true"
>>> raw_data = urllib2.urlopen(url).read()
>>> data = bz2.decompress(raw_data)
>>> df = pd.read_csv(io.BytesIO(data))
>>> df.head()
path st_mode st_ino \
0 /Users/hawk/Library/minecraft/bin/minecraft/mo... 33261 59612469
1 /Users/hawk/Library/Application Support/Google... 16832 91818463
2 /Users/hawk/Library/Caches/Metadata/Safari/His... 33188 95398522
3 /Users/hawk/Documents/Minecraft1.6.4/assets/so... 33188 90620503
4 /Users/hawk/Library/Caches/Metadata/Safari/His... 33188 96129272
st_dev st_nlink st_uid st_gid st_size st_atime st_mtime \
0 16777219 1 501 20 2626 1370201925 1366983504
1 16777219 3 501 20 102 1391697388 1384638452
2 16777219 1 501 20 36758 1389032348 1389032363
3 16777219 1 501 20 12129 1387000073 1384114141
4 16777219 1 501 20 170 1390545751 1390545751
st_ctime
0 1368736019
1 1384638452
2 1389032363
3 1384114141
4 1390545751
[5 rows x 11 columns]
您希望传递相应的encoding
参数。