एक ज़िप फ़ाइल के अंदर एक CSV फ़ाइल को पढ़ने के लिए एक सरल कार्यक्रम अजगर 2.7 में काम करता है, लेकिन अजगर में 3,2py3k: आप एक ज़िप फ़ाइल के अंदर फ़ाइल को पाठ के रूप में कैसे पढ़ते हैं, बाइट्स नहीं?
$ 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?)
तो अजगर 3 में csv
मॉड्यूल एक पाठ फ़ाइल को देखना चाहता है नहीं है, लेकिन zipfile.ZipFile.open
एक zipfile.ZipExtFile
रिटर्न जिसे हमेशा बाइनरी डेटा के रूप में माना जाता है।
कोई इस काम को पायथन 3 में कैसे बनाता है?
io.TextIOWrapper() एफटीडब्ल्यू !!! धन्यवाद। –