[python] SyntaxError of Non-ASCII character

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

This question is related to python encoding xml-parsing lxml non-ascii-characters

The answer is


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:


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to encoding

How to check encoding of a CSV file UnicodeEncodeError: 'ascii' codec can't encode character at special name Using Javascript's atob to decode base64 doesn't properly decode utf-8 strings What is the difference between utf8mb4 and utf8 charsets in MySQL? The character encoding of the plain text document was not declared - mootool script UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 23: ordinal not in range(128) How to encode text to base64 in python UTF-8 output from PowerShell Set Encoding of File to UTF8 With BOM in Sublime Text 3 Replace non-ASCII characters with a single space

Examples related to xml-parsing

How to create XML file with specific structure in Java jQuery xml error ' No 'Access-Control-Allow-Origin' header is present on the requested resource.' SyntaxError of Non-ASCII character xml.LoadData - Data at the root level is invalid. Line 1, position 1 XML Error: Extra content at the end of the document How to use sed to extract substring How to fix Invalid byte 1 of 1-byte UTF-8 sequence The best node module for XML parsing Parsing XML with namespace in Python via 'ElementTree' oracle plsql: how to parse XML and insert into table

Examples related to lxml

pip is not able to install packages correctly: Permission denied error bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library? Flask example with POST SyntaxError of Non-ASCII character How to install lxml on Ubuntu builtins.TypeError: must be str, not bytes libxml install error using pip Installing lxml module in python How to select following sibling/xml tag using xpath

Examples related to non-ascii-characters

How do I remove all non-ASCII characters with regex and Notepad++? Remove non-ascii character in string SyntaxError of Non-ASCII character Find non-ASCII characters in varchar columns using SQL Server Replacing accented characters php (grep) Regex to match non-ASCII characters?