DeprecationWarning: Non-ASCII character '\xf3'

J

jau

Hi co-listers!

I have been off Python for 2 years and now, that i'm used to Eclipse and
Java, I decided to start a project with Python to refresh skills this
time using Eclipse and TrueStudio. But now, two things can be occured
since the last time i used it.

the first one, something concerning to the encoding has changed and i
haven't noticed it.

the other one, when using Python from Eclipse i have to add any special
config lines at the begining of my Python files.

if i have this hello world python "program" (i have to call it by
someway, hahaha)

print "hello world"

i get this output

hello world
sys:1: DeprecationWarning: Non-ASCII character '\xf3' in file
C:\Workspace\J&J\src\es\jau\main.py on line 2, but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details

the article mentioned above didn't explain so much for me.

i doesn't look to be an error, but curiosity is bitting me... what's
really happening here? Do I need to do any special thing to avoid this?

Thanks everyone!

Salut i república
 
F

Fredrik Lundh

jau said:
print "hello world"

i get this output

hello world
sys:1: DeprecationWarning: Non-ASCII character '\xf3' in file
C:\Workspace\J&J\src\es\jau\main.py on line 2, but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details

the article mentioned above didn't explain so much for me.

the error message means that Python found an \xF3-character
on the second line in your main program (\xF3 is usually "latin
small letter o with acute", or "ó")

the document you were referred to says

Python will default to ASCII as standard encoding if no other
encoding hints are given.

(in other words, Python wants you to use plain ASCII in your source
code, unless otherwise specified).

and continues

To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file:

#!/usr/bin/python
# -*- coding: <encoding name> -*-

(in other words, if you want to keep using non-ASCII characters, you
need to add a line like

# -*- coding: iso-8859-1 -*-

and make sure it's the first or second line in your script. if you didn't
mean to add it, just get rid of it and try again. if eclipse doesn't
display
the character, check the file using notepad or some other editor)

</F>
 
B

Benjamin Niemann

jau said:
Hi co-listers!

I have been off Python for 2 years and now, that i'm used to Eclipse and
Java, I decided to start a project with Python to refresh skills this
time using Eclipse and TrueStudio. But now, two things can be occured
since the last time i used it.

the first one, something concerning to the encoding has changed and i
haven't noticed it.

the other one, when using Python from Eclipse i have to add any special
config lines at the begining of my Python files.

if i have this hello world python "program" (i have to call it by
someway, hahaha)

print "hello world"

i get this output

hello world
sys:1: DeprecationWarning: Non-ASCII character '\xf3' in file
C:\Workspace\J&J\src\es\jau\main.py on line 2, but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details

the article mentioned above didn't explain so much for me.

i doesn't look to be an error, but curiosity is bitting me... what's
really happening here? Do I need to do any special thing to avoid this?

The single print statement above should not trigger this warning. Are there
any non-ASCII character in the file (perhaps comments?).
If you use non-ASCII characters in python source files, you have to tell the
interpreter which encoding you are using - otherwise funny things will
happen. You have to find out which encoding Eclipse uses to save file
(looks like iso-8859-1) and add the line

# -*- coding: iso-8859-1 -*-

at the top of the file.

Python does not make any assumptions about the encoding beyond US-ASCII
(perhaps it does, but it does not encourage taking advantage of this) - if
you use anything else, then you'll have to declare it.

This will be important, if you use unicode strings:

u"stränge characters"

Python must know the encoding of the file in order to decode the string
literal into an unicode string.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top