UnicodeDecodeError 读取 CSV 中的字符串
我在读取 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') 但这也不起作用。
I'm having a problem reading some chars in python.
I have a csv file in UTF-8 format, and I'm reading, but when script read:
Preußen Münster-Kaiserslautern II
I get this error:
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)
I tried to use Unicode functions and convert string to Unicode, but I haven't found the solution. I tried to use sys.setdefaultencoding('utf8')
but that doesn't work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试 csv 模块文档中描述的
unicode_csv_reader()
生成器。Try the
unicode_csv_reader()
generator described in the csv module docs.