0

I want to read in a txt file that sits in a folder within a zipped folder as a pandas data frame.

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.

The problem is I get a KeyError message with my code.

I think it's because my txt file sits in a folder within a folder?

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"
Robbie
  • 275
  • 4
  • 20

1 Answers1

0

You need to add full path to the file:

txt_raw = 'hcc-survival/hcc-data.txt'
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38