Discussion:
Sudden error: SyntaxError: Non-ASCII character '\xc2' in file
Gnarlodious
2011-03-30 14:34:46 UTC
Permalink
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
Benjamin Kaplan
2011-03-30 14:52:27 UTC
Permalink
Post by 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.
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.
Post by Gnarlodious
--
http://mail.python.org/mailman/listinfo/python-list
Peter Otten
2011-03-30 15:28:10 UTC
Permalink
Post by 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.
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...
Gnarlodious
2011-03-30 23:58:27 UTC
Permalink
Post by Peter Otten
You are trying to run your 3.x code with Python 2.x...
You're right. Exactly why this started happening I don't know.

Thanks.

-- Gnarlie
Terry Reedy
2011-03-31 02:36:43 UTC
Permalink
Post by Gnarlodious
Post by Peter Otten
You are trying to run your 3.x code with Python 2.x...
You're right. Exactly why this started happening I don't know.
I believe recent Mac OSX comes with some 2.x installed as the default
Python.
--
Terry Jan Reedy
Peter Otten
2011-03-30 15:30:53 UTC
Permalink
On Wed, Mar 30, 2011 at 10:34 AM, Gnarlodious <gnarlodious at gmail.com>
Post by 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 &middot;
character.
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.
All good advice except that Python 3 defaults to UTF-8 not ASCII as its
source encoding.
eryksun ()
2011-03-30 16:32:18 UTC
Permalink
Post by Gnarlodious
The error originates at '?' which string contains a &middot;
character.
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
A middle dot is Unicode \x00\xb7, which maps to UTF-8 \xc2\xb7. According to PEP 3120 the default source encoding for Python 3.x is UTF-8. (I'll take their word for it, since I'm still using 2.7). Are you declaring an ASCII encoding (e.g. # coding: ascii)? If not, are you sure that you're running in 3.x?
Loading...