[英]Reading in txt file as pandas dataframe from a folder within a zipped folder
I want to read in a txt file that sits in a folder within a zipped folder as a pandas data frame.我想读取位于压缩文件夹内文件夹中的 txt 文件作为 pandas 数据框。
I've looked at how to read in a txt file and how to access a file from within a zipped folder, Load data from txt with pandas and Download Returned Zip file from URL respectively.我已经查看了如何读取 txt 文件以及如何从压缩文件夹中访问文件,分别使用 pandas 从 txt 加载数据和从 URL 下载返回的 Zip 文件。
The problem is I get a KeyError
message with my code.问题是我的代码收到一条
KeyError
消息。
I think it's because my txt file sits in a folder within a folder?我认为这是因为我的 txt 文件位于文件夹中的文件夹中?
Thanks for any help!谢谢你的帮助!
# MWE
import requests
import pandas as pd
from zipfile import ZipFile
from io import BytesIO
txt_raw = 'hcc-data.txt'
zip_raw = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00423/hcc-survival.zip'
r = requests.get(zip_raw)
files = ZipFile(BytesIO(r.content))
df_raw = pd.read_csv(files.open(txt_raw), sep=",", header=None)
# ERROR
KeyError: "There is no item named 'hcc-data.txt' in the archive"
You need to add full path to the file:您需要添加文件的完整路径:
txt_raw = 'hcc-survival/hcc-data.txt'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.