在python中读取csv压缩文件 [英] Reading csv zipped files in python

查看:57
本文介绍了在python中读取csv压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从压缩的 csv 文件中获取数据.有没有办法在不解压缩整个文件的情况下做到这一点?如果没有,我如何解压缩文件并有效地读取它们?

I'm trying to get data from a zipped csv file. Is there a way to do this without unzipping the whole files? If not, how can I unzip the files and read them efficiently?

推荐答案

我使用 zipfile 模块将 ZIP 直接导入到 Pandas 数据帧.假设文件名为intfile";它位于名为THEZIPFILE"的 .zip 文件中:

I used the zipfile module to import the ZIP directly to pandas dataframe. Let's say the file name is "intfile" and it's in .zip named "THEZIPFILE":

import pandas as pd
import zipfile

zf = zipfile.ZipFile('C:/Users/Desktop/THEZIPFILE.zip') 
df = pd.read_csv(zf.open('intfile.csv'))

这篇关于在python中读取csv压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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