Search code examples
csvpython-3.xzip

How do you read a file inside a zip file as text, not bytes?


A simple program for reading a CSV file inside a ZIP archive:

import csv, sys, zipfile

zip_file    = zipfile.ZipFile(sys.argv[1])
items_file  = zip_file.open('items.csv', 'rU')
    
for row in csv.DictReader(items_file):
    pass

works in Python 2.7:

$ python2.7 test_zip_file_py3k.py ~/data.zip
$

but not in Python 3.2:

$ python3.2 test_zip_file_py3k.py ~/data.zip
Traceback (most recent call last):
    File "test_zip_file_py3k.py", line 8, in <module>
    for row in csv.DictReader(items_file):
    File "/somedir/python3.2/csv.py", line 109, in __next__
    self.fieldnames
    File "/somedir/python3.2/csv.py", line 96, in fieldnames
    self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (did you open the file 
in text mode?)

The csv module in Python 3 wants to see a text file, but zipfile.ZipFile.open returns a zipfile.ZipExtFile that is always treated as binary data.

How does one make this work in Python 3?


Solution

  • I just noticed that Lennart's answer didn't work with Python 3.1, but it does work with Python 3.2. They've enhanced zipfile.ZipExtFile in Python 3.2 (see release notes). These changes appear to make zipfile.ZipExtFile work nicely with io.TextWrapper.

    Incidentally, it works in Python 3.1, if you uncomment the hacky lines below to monkey-patch zipfile.ZipExtFile, not that I would recommend this sort of hackery. I include it only to illustrate the essence of what was done in Python 3.2 to make things work nicely.

    $ cat test_zip_file_py3k.py 
    import csv, io, sys, zipfile
    
    zip_file    = zipfile.ZipFile(sys.argv[1])
    items_file  = zip_file.open('items.csv', 'rU')
    # items_file.readable = lambda: True
    # items_file.writable = lambda: False
    # items_file.seekable = lambda: False
    # items_file.read1 = items_file.read
    items_file  = io.TextIOWrapper(items_file)
        
    for idx, row in enumerate(csv.DictReader(items_file)):
        print('Processing row {0} -- row = {1}'.format(idx, row))
    

    If I had to support py3k < 3.2, then I would go with the solution in my other answer.

    Update for 3.6+

    Starting w/3.6, support for mode='U' was removed^1:

    Changed in version 3.6: Removed support of mode='U'. Use io.TextIOWrapper for reading compressed text files in universal newlines mode.

    Starting w/3.8, a Path object was added which gives us an open() method that we can call like the built-in open() function (passing newline='' in the case of our CSV) and we get back an io.TextIOWrapper object the csv readers accept. See Yuri's answer, here.