Sudden error: SyntaxError: Non-ASCII character '\xc2' in file

G

Gnarlodious

RSS script runs fine on my dev machine but errors on the server
machine. Script was last run 3 days ago with no problem. Possible
clue: dev machine is (Mac OSX) running Python 3.1.1 while server is
running Python 3.1.3. I have not updated anything that should suddenly
cause this error starting yesterday.

The error originates at '·' which string contains a ·
character.

Complete error message is:

SyntaxError: Non-ASCII character '\xc2' in file /Library/WebServer/
Sites/Sectrum/Site/Feed.py on line 17, but no encoding declared; see
http://www.python.org/peps/pep-0263.html for details

Any help how to fix this and why it suddenly started erroring 2 days
ago...

-- Gnarlie
 
B

Benjamin Kaplan

RSS script runs fine on my dev machine but errors on the server
machine. Script was last run 3 days ago with no problem. Possible
clue: dev machine is (Mac OSX) running Python 3.1.1 while server is
running Python 3.1.3. I have not updated anything that should suddenly
cause this error starting yesterday.

The error originates at '·' which string contains a ·
character.

Complete error message is:

SyntaxError: Non-ASCII character '\xc2' in file /Library/WebServer/
Sites/Sectrum/Site/Feed.py on line 17, but no encoding declared; see
http://www.python.org/peps/pep-0263.html for details

Any help how to fix this and why it suddenly started erroring 2 days
ago...

-- Gnarlie

You don't have a &middot character. Your computer doesn't understand
"characters". You have the byte sequence \xc2\xb7. When you have a
Unicode string (the default in Python 3), Python needs some way of
converting the byte sequence to a character sequence. The way it does
that is through the encoding. But you don't have an encoding
specified, so rather than guess, Python is falling back on the lowest
common denominator: ASCII, which doesn't understand the byte \xc2-
hence the error.

To fix this, just put the line
# coding=utf-8
at the very top of the code file.
 
P

Peter Otten

Gnarlodious said:
RSS script runs fine on my dev machine but errors on the server
machine. Script was last run 3 days ago with no problem. Possible
clue: dev machine is (Mac OSX) running Python 3.1.1 while server is
running Python 3.1.3. I have not updated anything that should suddenly
cause this error starting yesterday.

The error originates at '·' which string contains a ·
character.

Complete error message is:

SyntaxError: Non-ASCII character '\xc2' in file /Library/WebServer/
Sites/Sectrum/Site/Feed.py on line 17, but no encoding declared; see
http://www.python.org/peps/pep-0263.html for details

Any help how to fix this and why it suddenly started erroring 2 days
ago...

You are trying to run your 3.x code with Python 2.x...
 
P

Peter Otten

Benjamin said:
You don't have a &middot character. Your computer doesn't understand
"characters". You have the byte sequence \xc2\xb7. When you have a
Unicode string (the default in Python 3), Python needs some way of
converting the byte sequence to a character sequence. The way it does
that is through the encoding. But you don't have an encoding
specified, so rather than guess, Python is falling back on the lowest
common denominator: ASCII, which doesn't understand the byte \xc2-
hence the error.

To fix this, just put the line
# coding=utf-8
at the very top of the code file.

All good advice except that Python 3 defaults to UTF-8 not ASCII as its
source encoding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top