Reputation: 363
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
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