使用python-3.x从zip存档中读取CSV文件 [英] Reading CSV files from zip archive with python-3.x
问题描述
我有一个压缩的存档,其中包含几个csv
文件.
I have a zipped archive that contains several csv
files.
例如,假设myarchive.zip
包含myfile1.csv
,myfile2.csv
,myfile3.csv
在python 2.7
中,我可以使用
import pandas as pd
import zipfile
with zipfile.ZipFile(myarchive.zip, 'r') as zippedyear:
for filename in ['myfile1.csv', 'myfile2.csv', 'myfile3.csv']:
mydf = pd.read_csv(zippedyear.open(filename))
现在与Python 3
做同样的事情会引发错误
Now doing the same thing with Python 3
throws the error
ParserError:迭代器应返回字符串,而不是字节(您是否打开过? 文本模式下的文件?)
ParserError: iterator should return strings, not bytes (did you open the file in text mode?)
我在这里不知所措.知道是什么问题吗? 谢谢!
I am at a loss here. Any idea what is the issue? Thanks!
推荐答案
确实很奇怪,因为您可以指定的唯一模式是r/w
(字符模式).
Strange indeed, since the only mode you can specify is r/w
(character modes).
这是一种解决方法;使用file.read
读取文件,将数据加载到StringIO
缓冲区中,然后将其传递给read_csv
.
Here's a workaround; read the file using file.read
, load the data into a StringIO
buffer, and pass that to read_csv
.
from io import StringIO
with zipfile.ZipFile(myarchive.zip, 'r') as zippedyear:
for filename in ['myfile1.csv', 'myfile2.csv', 'myfile3.csv']:
with zippedyear.open(filename) as f:
mydf = pd.read_csv(io.StringIO(f.read()))
这篇关于使用python-3.x从zip存档中读取CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!