Python 3.1 csv with gzip

dryfish john at dryfish.org
Fri Sep 11 13:13:48 CEST 2009


Python 3.1.1 doesn't seem to be happy with the use of gzip.open with
csv.reader.

Using this:

import gzip, csv, sys

data = csv.reader(gzip.open(sys.argv[1]))
for row in data:
    print(row)

Will give this:

Traceback (most recent call last):
  File "./a.py", line 6, in <module>
    for row in data:
_csv.Error: iterator should return strings, not bytes (did you open
the file in text mode?)

My work around is:

import gzip, csv, sys

def gziptext(filename):
    for line in gzip.open(filename):
        yield str(line, 'ascii')

data = csv.reader(gziptext(sys.argv[1]))
for row in data:
    print(row)




More information about the Python-list mailing list