【发布时间】:2016-02-18 09:09:27
【问题描述】:
当我使用默认版本的 Python (2.7.6) 在 Cloud9 IDE 中运行我的 python 代码时运行以下代码时出现以下错误:
import urllib
artistValue = "Sigur Rós"
artistValueUrl = urllib.quote(artistValue)
SyntaxError: 第 2 行 /home/ubuntu/workspace/test.py 文件中的非 ASCII 字符“\xc3”,但未声明编码;详情见http://www.python.org/peps/pep-0263.html
我阅读以适应以下代码是一种解决方法。
import urllib
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
artistValue = "Sigur Rós"
artistValueUrl = urllib.quote(artistValue)
当我尝试这个时,一个红色的 x 弹出错误显示为:
模块 'sys' 没有 'setdefaultencoding' 成员"
如果我运行代码,我仍然会收到语法错误。
为什么会发生这种情况,我该怎么办?
编辑: 我还从所选答案中尝试了以下内容:
import urllib
print urllib.quote(u"Sigur Rós")
当我运行它时,我收到以下错误:
SyntaxError: 文件中的非 ASCII 字符 '\xc3' /home/ubuntu/workspace/test.py 在第 2 行,但没有声明编码; 详情见http://www.python.org/peps/pep-0263.html
【问题讨论】:
-
抱歉,
sys.setdefaultencoding('utf-8')在 Cloud9 IDE 中不起作用;有关详细信息,请参阅the sys docs。无论如何,这不是一个好主意,请参阅Dangers of sys.setdefaultencoding('utf-8') 了解信息。请发布您的代码(在代码块中)和您尝试读取的数据示例(也在代码块中),以便我们帮助您解决问题。还要提及您使用的 Python 版本,因为 Python 2 和 Python 3 处理 Unicode 的方式不同。 -
感谢您的反馈。我已尽力编辑您上面推荐的内容。你有什么建议吗?
标签: python unicode cloud9-ide python-unicode