SyntaxError of Non-ASCII character [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-11-26 09:20:00

问题


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..


回答1:


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


来源:https://stackoverflow.com/questions/18078851/syntaxerror-of-non-ascii-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!