非 ASCII 字符的语法错误 [英] SyntaxError of Non-ASCII character
本文介绍了非 ASCII 字符的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析包含一些非 ASCII 字符的 xml,
I am trying to parse xml which contains the some non ASCII cheracter,
代码如下
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 = ...'行上显示错误喜欢
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
在终端中它正在工作,但在 eclipse IDE 上运行时它给了我一个错误.
in the terminal it's working but while running on the eclipse IDE it's giving me a error.
不知道如何克服..
推荐答案
您应该定义源代码编码,将其添加到您的脚本顶部:
You should define source code encoding, add this to the top of your script:
# -*- coding: utf-8 -*-
它在控制台和 IDE 中的工作方式不同的原因可能是因为设置了不同的默认编码.您可以通过运行来检查它:
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()
另见:
这篇关于非 ASCII 字符的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文