CSV中读取字符串时出现UnicodeDecodeError错误

3

我在使用Python时遇到了一些字符读取问题。

我有一个UTF-8格式的csv文件,但是当脚本读取时:

Preußen Münster-Kaiserslautern II

我遇到了这个错误:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 515, in __call__
    handler.get(*groups)
  File "/Users/fermin/project/gae/cuotastats/controllers/controllers.py", line 50, in get
    f.name = unicode( row[1])
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)

我尝试使用Unicode函数将字符串转换为Unicode,但是我没有找到解决方案。我尝试使用sys.setdefaultencoding('utf8'),但也不起作用。

1个回答

7
尝试使用在csv模块文档中描述的unicode_csv_reader()生成器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接