简体   繁体   English

Python 使用文件字节访问 zip 文件并遍历每个文件以将它们保存到数据帧中返回找不到文件的错误

[英]Python access a zip file using file bytes and loop over each file to save them into data frames is returning an error of file is not found

I am calling an API which will result in a zip file that might contain multiple CSV files:我正在调用 API 这将导致 zip 文件可能包含多个 CSV 文件:

import zipfile
from io import BytesIO

api_url = res.json()['export_url']
new_res = requests.get(api_url, auth=(user, pass))
filebytes = BytesIO(new_res.content)
myzipfile = zipfile.ZipFile(filebytes)
a = myzipfile.extractall
for name in myzipfile.namelist():
    print(name)

I can clearly see the file names but can't read them into data frame each one of them:我可以清楚地看到文件名,但无法将它们分别读入数据框:

for name in myzipfile.namelist():
    df = pd.read_csv(name)

The error is:错误是:

FileNotFoundError: [Errno 2] File data.csv does not exist: 'data.csv'

I tried:我试过了:

for name in myzipfile.printdir():
    print(name)

and read as csv but didn't work.并读作 csv 但没有用。

The file is still zipped - you cannot just read the contained file as you would normally.该文件仍处于压缩状态 - 您不能像往常一样读取包含的文件。 Zipfile has its own open function for reading contained files. Zipfile 有自己的open function 用于读取包含的文件。 You can then read the data into a dataframe with pandas.然后,您可以使用 pandas 将数据读入 dataframe。

for name in myzipfile.namelist():
    with myzipfile.open(name) as myfile:
        df = pd.read_csv(myfile)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM