Python 从压缩文件夹中的文件夹中读取txt文件作为数据帧
我想读入一个txt文件,该文件位于压缩文件夹中的一个文件夹中,作为数据帧 我分别研究了如何读取txt文件和如何从压缩文件夹中访问文件 问题是我的代码中有一条Python 从压缩文件夹中的文件夹中读取txt文件作为数据帧,python,pandas,data-science,zipfile,Python,Pandas,Data Science,Zipfile,我想读入一个txt文件,该文件位于压缩文件夹中的一个文件夹中,作为数据帧 我分别研究了如何读取txt文件和如何从压缩文件夹中访问文件 问题是我的代码中有一条KeyError消息 我想这是因为我的txt文件位于文件夹中的一个文件夹中 谢谢你的帮助 # MWE import requests import pandas as pd from zipfile import ZipFile from io import BytesIO txt_raw = 'hcc-data.txt' zip_ra
KeyError
消息
我想这是因为我的txt文件位于文件夹中的一个文件夹中
谢谢你的帮助
# 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"
您需要将完整路径添加到文件:
txt_raw = 'hcc-survival/hcc-data.txt'
zip不包含“hcc data.txt”-请检查zip结构