我在读取 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')
但这也不起作用。
最佳答案
尝试使用 csv module docs 中描述的 unicode_csv_reader()
生成器.
关于python - UnicodeDecodeError 读取 CSV 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4923929/