简体   繁体   English

从压缩文件夹中的文件夹中读取 txt 文件 pandas dataframe

[英]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.

相关问题 从压缩文件夹中的文件夹中读取 H5File 到 pandas dataframe - Read H5File from a folder inside a zipped folder into pandas dataframe 如何将 CSV 文件从 URL 的压缩文件夹加载到 Pandas ZBA834BA059A91A7EB88E2 - How to Load a CSV File from zipped folder from URL into Pandas DataFrame 将压缩的 txt 文件读取为 pandas dataframe - Read zipped txt file as pandas dataframe 从python脚本的文件夹中读取.txt文件 - reading a .txt file from a folder in python script 从文件夹读取 file.txt 到其他文件夹会出错 - Reading file .txt from folder to other folder give error 如何解压缩所有以.zip结尾的文件夹/文件,并从每个压缩的文件夹中提取“ file.txt”文件 - How to unzip all folders/files that end in .zip and extract “file.txt” file from each zipped folder 读取一个文件夹中的多个文件并创建一个熊猫数据框 - reading multiple files in a folder and creating a pandas dataframe 从带有子文件夹的压缩文件夹中提取.txt 文件并使用文件夹位置重命名文件 - Extract .txt files from zipped folder with subfolders and rename files with folder location 将压缩文件作为 Pandas DataFrame 读取 - Read a zipped file as a pandas DataFrame 如何从压缩文件中读取 json 到 pandas dataframe? - How to read json to pandas dataframe from a zipped file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM