Syntax Error non-ASCII character

I am trying to do an xml parsing that contains some illiterate ASCII,

the code looks 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 an error in 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 

it works in the terminal, but while working on the Eclipse IDE it gives me an error.

I don’t know how to win.

+65
python encoding xml-parsing lxml non-ascii-characters
Aug 6 '13 at 11:23
source share
1 answer

You must determine the encoding of the source code, add this to the beginning of your script:

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

The reason it works differently in the console and in the IDE is most likely due to the different default encoding settings. You can check it by doing:

 import sys print sys.getdefaultencoding() 

See also:

  • Why declare unicode by string in python?
  • Changing the default encoding for Python?
  • The correct way to determine the encoding of Python source code
+152
Aug 6 '13 at 11:24 on
source share
β€” -



All Articles