Python 3.1 csv with gzip

D

dryfish

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)
 
S

Stefan Behnel

dryfish said:
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?)

See codecs.EncodedFile().

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top