Un sencillo programa para la lectura de un archivo CSV en un archivo zip funciona en Python 2.7, pero no en Python 3,2py3k: ¿Cómo se lee un archivo dentro de un archivo zip como texto, no como bytes?
$ 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?)
Así el módulo csv
en Python 3 quiere ver un archivo de texto, pero zipfile.ZipFile.open
devuelve un zipfile.ZipExtFile
eso siempre se trata como datos binarios.
¿Cómo funciona esto en Python 3?
io.TextIOWrapper() FTW !!! Gracias. –