【问题标题】:Reading in txt file as pandas dataframe from a folder within a zipped folder从压缩文件夹中的文件夹中读取 txt 文件作为 pandas 数据框
【发布时间】:2020-09-22 12:12:26
【问题描述】:

我想读取一个 txt 文件,该文件位于压缩文件夹中的文件夹中,作为 pandas 数据框。

我已经了解了如何读取 txt 文件以及如何从压缩文件夹中访问文件,分别为 Load data from txt with pandasDownload Returned Zip file from URL

问题是我的代码收到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"

【问题讨论】:

  • 压缩包不包含 'hcc-data.txt' - 检查压缩包结构

标签: python pandas data-science zipfile


【解决方案1】:

您需要添加文件的完整路径:

txt_raw = 'hcc-survival/hcc-data.txt'

【讨论】:

    猜你喜欢
    • 2020-01-28
    • 2021-05-30
    • 1970-01-01
    • 2020-05-06
    • 2018-02-23
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多