使用 pandas 读取zip文件中包含的多个文件 [英] reading multiple files contained in a zip file with pandas
本文介绍了使用 pandas 读取zip文件中包含的多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有多个zip文件,其中包含不同类型的txt文件. 如下所示:
I have multiple zip files containing different types of txt files. Like below:
zip1
- file1.txt
- file2.txt
- file3.txt
如何使用熊猫读取每个文件而不提取它们?
How can I use pandas to read in each of those files without extracting them?
我知道每个zip文件是否为1个文件,我可以对read_csv使用压缩方法,如下所示:
I know if they were 1 file per zip I could use the compression method with read_csv like below:
df = pd.read_csv(textfile.zip, compression='zip')
任何有关此操作的帮助都将非常有用.
Any help on how to do this would be great.
推荐答案
您可以将ZipFile.open()
传递给pandas.read_csv()
,以便从打包为多文件zip
的csv文件构造pandas.DataFrame
.>
代码:
You can pass ZipFile.open()
to pandas.read_csv()
to construct a pandas.DataFrame
from a csv-file packed into a multi-file zip
.
pd.read_csv(zip_file.open('file3.txt'))
将所有.csv
读入字典的示例:
Example to read all .csv
into a dict:
from zipfile import ZipFile
zip_file = ZipFile('textfile.zip')
dfs = {text_file.filename: pd.read_csv(zip_file.open(text_file.filename))
for text_file in zip_file.infolist()
if text_file.filename.endswith('.csv')}
这篇关于使用 pandas 读取zip文件中包含的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文