在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'))