py3k: ¿Cómo se lee un archivo dentro de un archivo zip como texto, no bytes?
Un programa simple para leer un archivo CSV dentro de un archivo zip funciona en Python 2.7, pero no en Python 3.2
$ cat test_zip_file_py3k.py
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
$ python2.7 test_zip_file_py3k.py ~/data.zip
$ 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 "/home/msabramo/run/lib/python3.2/csv.py", line 109, in __next__
self.fieldnames
File "/home/msabramo/run/lib/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?)
Entonces elcsv
module en Python 3 quiere ver un archivo de texto, perozipfile.ZipFile.open
devuelve unzipfile.ZipExtFile
que siempre se trata como datos binarios.
¿Cómo se hace que esto funcione en Python 3?