Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从压缩文件夹中的文件夹中读取txt文件作为数据帧_Python_Pandas_Data Science_Zipfile - Fatal编程技术网

Python 从压缩文件夹中的文件夹中读取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

我想读入一个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_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结构