我正在尝试使用csv模块读取utf-8 csv文件,由于编码,我在创建python 2和3的通用代码时遇到了一些麻烦.
这是Python 2.7中的原始代码:
with open(filename, 'rb') as csvfile:
csv_reader = csv.reader(csvfile, quotechar='\"')
langs = next(csv_reader)[1:]
for row in csv_reader:
pass
Run Code Online (Sandbox Code Playgroud)
但是当我用python 3运行它时,它不喜欢我没有"编码"打开文件的事实.我试过这个:
with codecs.open(filename, 'r', encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile, quotechar='\"')
langs = next(csv_reader)[1:]
for row in csv_reader:
pass
Run Code Online (Sandbox Code Playgroud)
现在python 2无法解码"for"循环中的行.那么......我该怎么做?
我搜索了一下但我找不到一个tuto使用PIL和PyPy.根据PyPy的博客,PIL得到了支持.
我该怎么办?
编辑:我在Windows 7 x64(python 2.7.1 32bits)上运行
这里是追溯(pypy 1.4.1 windows二进制):
Traceback (most recent call last):
File "app_main.py", line 53, in run_toplevel
File "tools\python\gen_images.py", line 52, in <module>
main()
File "tools\python\gen_images.py", line 44, in main
image = Image.open(file)
File "d:\pypy\site-packages\PIL\Image.py", line 1965, in open
return factory(fp, filename)
File "d:\pypy\site-packages\PIL\ImageFile.py", line 91, in __init__
self._open()
File "d:\pypy\site-packages\PIL\GifImagePlugin.py", line 97, in _open
self.seek(0) # get ready to read first frame
File "d:\pypy\site-packages\PIL\GifImagePlugin.py", line 152, in seek …Run Code Online (Sandbox Code Playgroud)