非ASCII字符的SyntaxError [英] SyntaxError of Non-ASCII character
本文介绍了非ASCII字符的SyntaxError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析包含一些非ASCII密码的xml,
代码如下所示
from lxml import etree
from lxml import objectify
content = u'<?xml version =1.0encoding =utf-8?> < div>订单日期:05/08/2013 12:24:28< / div>'
mail.replace('\xa0','')
xml = etree.fromstring )
但它显示在'content = ...'线上错误
喜欢
syntaxError:第3行文件/home/projects/ztest/responce.py中的非ASCII字符'\xc2' ,
但没有编码声明;有关详细信息,请参阅http://www.python.org/peps/pep-0263.html
终端它的工作,但是在eclipse IDE上运行它给我一个错误。
不知道如何克服..
<您应该定义源代码编码,将其添加到脚本的顶部:
# - * - 编码:utf-8 - * -
它在控制台中的作用不同,在IDE中可能是因为设置了不同的默认编码。您可以执行以下操作:
import sys
打印sys.getdefaultencoding()
另见:
I am trying to parse xml which contains the some non ASCII cheracter,
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.
Don't know how to overcome..
解决方案
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:
import sys
print sys.getdefaultencoding()
Also see:
- Why declare unicode by string in python?
- Changing default encoding of Python?
- Correct way to define Python source code encoding
这篇关于非ASCII字符的SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文