SyntaxError: Non-ASCII character '\xc2' in file.py

5,808 views
Skip to first unread message

NikunjBadjatya

unread,
Jan 7, 2010, 11:43:20 PM1/7/10
to python-excel
Hi,
I have a list of lists containing texts to be filled into
corresponding rows and cols of xl sheet using .write function.
ex.data=[
[' 1 ','LTP Execution Procedure',' '],
[' ',' ',' '],
[' ','Prerequisite :',' '],
[' ',' ',' Install LTP on host with root login for testing with
glibc'], #line X
['','','# rpm -ivh arm-sony-linux-dev-ltp-20060822-CE_1.5.arm.rpm'],
[' ',' ',' LTP on host with root login for testing with sslibc
'] #line Y
]

I am passing this list into myfun(), which does the job of writing the
above data[] into the rows and columns. I am using xlwt module.

I am stuck with the following error:
SyntaxError: Non-ASCII character '\xc2' in file.py on line X and line
Y , but no encoding declared.

Any idea what is wrong with my code??
Thanks

John Machin

unread,
Jan 8, 2010, 1:00:48 AM1/8/10
to python...@googlegroups.com
On 8/01/2010 3:43 PM, NikunjBadjatya wrote:
> Hi,

Hello again. Did you get your hyperlinks working crossplatformly?

Exactly what the the error message says.

See
http://docs.python.org/reference/lexical_analysis.html#encoding-declarations
(new in Python 2.5) for documentation and
http://www.python.org/dev/peps/pep-0263/ for background.

In each of line X and line Y, there are two non-ASCII characters. What
appears to be a SPACE ("\x20") after each of "LTP" and "login" is
actually a NO-BREAK SPACE (U+00A0) which is "\xc2\xa0" when encoded in
UTF-8.

Note: I'm viewing the unrendered source of your message in Thunderbird;
the message has

Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

in the headers, and I'm seeing e.g.

Install LTP=A0on host with root login=A0for

The evidence ("Non-ASCII character '\xc2'") is consistent with your file
having been encoded in UTF-8 at the time of the syntax error.

If you really want the NO-BREAK SPACEs in there:

(1) code them explicitly using escape sequences e.g.

text = "foo\xc2\xa0bar\xc2\xa0zot"

or

NBSP = "\xc2\xa0"

text = NBSP.join(["foo", "bar", "zot"])

Then you can put an encoding declaration

# coding: utf-8

at the top of your file as documentation (you won't get a Syntax error
without this, as there will be no non-ASCII bytes in your file before
Python converts the escape sequences).

or
(2) insert an encoding declaration at the top of your file

# coding: utf-8

and leave the NBSPs where they are. Note: NOT RECOMMENDED. You see space
but you've got no-break space. Explicit is better.

However if you didn't expect the no-break spaces (or any other non-ASCII
characters), then edit them to become spaces, and don't put an encoding
declaration at the top of your file. That way if any more nassssties
creep in, you'll get a syntax error, and you can eradicate them.

BTW, a blanket assertion: A Python syntax error in one of your scripts
can not possibly be caused by a problem in any module that your script
might be able to import in the future once your script is repaired
sufficiently to make it runnable.

HTH,
John

Reply all
Reply to author
Forward
0 new messages