Python 非 ASCII 字符的语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18078851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:52:18  来源:igfitidea点击:

SyntaxError of Non-ASCII character

pythonencodingxml-parsinglxmlnon-ascii-characters

提问by OpenCurious

I am trying to parse xml which contains the some non ASCII cheracter,

我正在尝试解析包含一些非 ASCII 字符的 xml,

the code looks like below

代码如下所示

from lxml import etree
from lxml import objectify
content = u'<?xml version="1.0" encoding="utf-8"?><div>Order date ? ? ? ? ? ? ? ? ? ? ? ? ? ?: 05/08/2013 12:24:28</div>'
mail.replace('\xa0',' ')
xml = etree.fromstring(mail)

but it shows me error on the line 'content = ...' like

但它向我显示了“内容 = ...”行上的错误,例如

syntaxError: Non-ASCII character '\xc2' in file /home/projects/ztest/responce.py on line 3, 
but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

in the terminal it's working but while running on the eclipse IDE it's giving me a error.

在终端中它正在工作,但是在 eclipse IDE 上运行时它给了我一个错误。

Don't know how to overcome..

不知道怎么克服..

采纳答案by alecxe

You should define source code encoding, add this to the top of your script:

您应该定义源代码编码,将其添加到脚本的顶部:

# -*- coding: utf-8 -*-

The reason why it works differently in console and in the IDE is, likely, because of different default encodings set. You can check it by running:

它在控制台和 IDE 中的工作方式不同的原因可能是因为设置了不同的默认编码。您可以通过运行来检查它:

import sys
print sys.getdefaultencoding()

Also see:

另见: