首页 > 解决方案 > 从 Zip Link Pandas 读取多个文件

问题描述

我有一个包含多个 csv 文件的 zip 文件 URL 链接。

如何直接在 Pandas 中单独读取文件,而不是在下载到本地文件夹后?

标签: pandas

解决方案


也许试试这个作为一个例子......

from zipfile import ZipFile
from io import BytesIO
import urllib.request as urllib2
import pandas as pd

# get the zip file
url = urllib2.urlopen("http://www.example.com/aZipFileWithDifferentCSVFilesInIt.zip").read()
files = ZipFile(BytesIO(url))

# get the file names into a list; you could use this list for different things 
# and to get certain files etc.
listOfFilesNames = files.namelist()

# open a file from the list; in this case position 23.  This part could be a loop etc. 
# and if 'specificFileName' do something different etc.
myCSV = files.open(listOfFilesNames[23])

# create the dataframe
df = pd.read_csv(myCSV)

# check the output
print(df)

推荐阅读