Python 3.1 csv with gzip

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

Python 3.1.1 doesn't seem to be happy with the use of with

Using this:

import gzip, csv, sys

data = csv.reader([1]))
for row in data:

Will give this:

Traceback (most recent call last):
  File "./", 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
        yield str(line, 'ascii')

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

More information about the Python-list mailing list