小编Syl*_*Syl的帖子

Python 2和3 csv阅读器

我正在尝试使用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"循环中的行.那么......我该怎么做?

python csv encoding python-3.x

10
推荐指数
1
解决办法
9060
查看次数

如何在PyPy中使用PIL?

我搜索了一下但我找不到一个tuto使用PIL和PyPy.根据PyPy的博客,PIL得到了支持.

  • 我在PYTHONPATH中安装了PIL.
  • 下载后,pip制作2个.pyd文件:_imaging.pyd和_imagingmath.pyd.
  • 安装完成后,我将%PYTHONPATH%/ lib/site-packages/PIL复制到我的PyPy site-packages目录.
  • 当我运行我的脚本(使用PIL)时,它说它无法导入_imaging C模块.

我该怎么办?

编辑:我在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)

python pypy python-imaging-library

8
推荐指数
1
解决办法
8109
查看次数