Python 3.1 csv with gzip

Discussion in 'Python' started by dryfish, Sep 11, 2009.

  1. dryfish

    dryfish Guest

    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)
     
    dryfish, Sep 11, 2009
    #1
    1. Advertising

  2. dryfish wrote:
    > 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
     
    Stefan Behnel, Sep 11, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    0
    Views:
    675
    Michal Mikolajczyk
    Feb 13, 2004
  2. Skip Montanaro
    Replies:
    0
    Views:
    744
    Skip Montanaro
    Feb 13, 2004
  3. Tintin92
    Replies:
    1
    Views:
    1,758
    Andrew Thompson
    Feb 14, 2007
  4. jliu66
    Replies:
    0
    Views:
    532
    jliu66
    Oct 19, 2007
  5. sso
    Replies:
    20
    Views:
    2,711
    Martin Gregorie
    Apr 26, 2009
Loading...

Share This Page