Question : Unzip csv and read in same block of code

Hi, I was just wondering if it was possible to use the zipfile module in python to unzip a csv file and then process the file in order to create another csv with the csv writer. In other words, i'd like to skip the unzip process and process the file while its in temporary memory. See code for an example.

Thanks.

Paul
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
z = zipfile.ZipFile(rawDir + dailyzip, 'r')
 
for info in z.infolist():
    fname = info.filename
    data = z.read(fname)
    for r in data: #iterate through each row in the csv data in memory
             #do processing here before writing to new csv file.
Open in New Window Select All

Answer : Unzip csv and read in same block of code

If I understand you correctly, you want to bind the extracted data to the csv.reader() without extracting the content to the physical .csv file. The csv.reader() accepts or open file object or the list. This way, you may want to convert the read data to the list of lines and pass it to the csv.reader(). See the snippet. (You may process the row with your csv.writer().)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
import csv
import zipfile
 
zipfname = 'test.zip'
z = zipfile.ZipFile(zipfname, 'r')
 
for info in z.infolist():
    fname = info.filename
    if fname.endswith('.csv'):       # detect the .csv file
        content = z.read(fname)      # read the content 
        data = content.split('\n')   # split by lines to the list
        reader = csv.reader(data)    # and consume by the reader
        for row in reader:           # iterate through the rows
            print repr(row)
           
z.close()
Open in New Window Select All
Random Solutions  
 
programming4us programming4us