cyrus24
cyrus24

Reputation: 363

Read all csv files in a zip with respective csv filenames as dataframe variable names

I need to read all CSV files from a zip folder in such a way that each file is read into a separate dataframe, and the dataframe name is the same as the name of the CSV file. Below is what I have done so far, I am having difficulty with assigning the file reading to the filename. Any help is much appreciated!

import os
import zipfile

csvNameList = []
with zipfile.ZipFile('../data/999915.zip') as z:
    for filename in z.namelist():
        csvNameList.append(filename.split(".")[1])
        df_name = filename.split(".")[1]
        df_name = pd.read_csv(z.open(filename),
                              header=None, 
                              sep='\|\|@@##', 
                              na_values='\\N')

I want to be able to reference the CSVs as data frames of the names stored within the csvNameList variable.

Upvotes: 0

Views: 661

Answers (1)

RJ Adriaansen
RJ Adriaansen

Reputation: 9619

This will create a dictionary of dataframes:

with zipfile.ZipFile('../data/999915.zip') as z:
    dataframes = {filename.split(".")[1]: pd.read_csv(z.open(filename),header=None,sep='\|\|@@##',na_values='\\N') for filename in z.namelist()}

Upvotes: 1

Related Questions