I have a bunch of gzipped CSV files that I\'d like to open for inspection using Python\'s built in CSV reader. I\'d like to do this without having first to manually unzip t
a more complete solution:
import csv, gzip
class GZipCSVReader:
def __init__(self, filename):
self.gzfile = gzip.open(filename)
self.reader = csv.DictReader(self.gzfile)
def next(self):
return self.reader.next()
def close(self):
self.gzfile.close()
def __iter__(self):
return self.reader.__iter__()
now you can use it like this:
r = GZipCSVReader('my.csv')
for map in r:
for k,v in map:
print k,v
r.close()
EDIT: following the below comment, how about a simpler approach:
def gzipped_csv(filename):
with gzip.open(filename) as f:
r = csv.DictReader(f)
for row in r:
yield row
which let's you then
for row in gzipped_csv(filename):
for k, v in row:
print(k, v)