非 ASCII 字符的语法错误

我试图解析包含一些非 ASCII 字符的 xml,

代码如下所示

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)

但是它在‘ content = ...’一行显示了错误 喜欢

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

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

不知道如何克服. 。

221905 次浏览

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

# -*- 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:

import sys
print sys.getdefaultencoding()

参见: