使用python-3.x从zip存档中读取CSV文件 [英] Reading CSV files from zip archive with python-3.x

查看:116
本文介绍了使用python-3.x从zip存档中读取CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个压缩的存档,其中包含几个csv文件.

I have a zipped archive that contains several csv files.

例如,假设myarchive.zip包含myfile1.csvmyfile2.csvmyfile3.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屋!

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