By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,961 Members | 2,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,961 IT Pros & Developers. It's quick & easy.

about coding

P: n/a
Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
directive.

I'm using an accented characters language. Some of them are correctly
displayed while one doesn't. I've written :
-*- coding: utf-8 -*-

Is this wrong ?

Where can I find a pratical explanation about these encodings ?

Thanks.
Aug 6 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
cantabile wrote:
Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
directive.

I'm using an accented characters language. Some of them are correctly
displayed while one doesn't. I've written :
-*- coding: utf-8 -*-

Is this wrong ?
It depends. Are those characters encoded as UTF-8? Or, more likely, are
they encoded as ISO-8859-1?
Where can I find a pratical explanation about these encodings ?


http://www.amk.ca/python/howto/unicode
http://en.wikipedia.org/wiki/Character_encoding
http://www.joelonsoftware.com/articles/Unicode.html

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 6 '05 #2

P: n/a
cantabile wrote:
Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
directive.

I'm using an accented characters language. Some of them are correctly
displayed while one doesn't. I've written :
-*- coding: utf-8 -*-

Is this wrong ?

Where can I find a pratical explanation about these encodings ?


The coding line tell the interpreter to assume that the file is encoded as
utf-8. It's the job of the editor that you are using, to actually encode
the file as utf-8.
The coding directive uses a format that is automatically recognized by the
emacs editor. If you are using another editor, you'll have to search its
menus for the proper way to save files as utf-8.
--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Aug 6 '05 #3

P: n/a
Benjamin Niemann wrote:
cantabile wrote:

Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
directive.

I'm using an accented characters language. Some of them are correctly
displayed while one doesn't. I've written :
-*- coding: utf-8 -*-

Is this wrong ?

Where can I find a pratical explanation about these encodings ?

The coding line tell the interpreter to assume that the file is encoded as
utf-8. It's the job of the editor that you are using, to actually encode
the file as utf-8.
The coding directive uses a format that is automatically recognized by the
emacs editor. If you are using another editor, you'll have to search its
menus for the proper way to save files as utf-8.


or possibly the OP needs to nut out what is the encoding he already has,
and tell Python what that encoding is.
Aug 6 '05 #4

P: n/a
Robert Kern a écrit :
It depends. Are those characters encoded as UTF-8? Or, more likely, are
they encoded as ISO-8859-1?
Where can I find a pratical explanation about these encodings ?

http://www.amk.ca/python/howto/unicode
http://en.wikipedia.org/wiki/Character_encoding
http://www.joelonsoftware.com/articles/Unicode.html


I'm using emacs. I guess this is utf-8 encoded...
Thanks for the links :)
Aug 6 '05 #5

P: n/a
cantabile wrote:
Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
directive.

I'm using an accented characters language. Some of them are correctly
displayed while one doesn't. I've written :
-*- coding: utf-8 -*-

Is this wrong ?

Where can I find a pratical explanation about these encodings ?

Thanks.


You must know the encoding produced by your editor.

If you want to experiment, you could save a small file and run

od -Ax -tx1 thefile.py

to see exactly what codepoints appear. Compare those with the
charts at http://www.unicode.org/ and you can probably surmize
a good guess.
Aug 11 '05 #6

P: n/a
Paul Watson wrote:
cantabile wrote:
Hi, being a newbie in Python, I'm a bit lost with the '-*- coding :
-*-' directive.

I'm using an accented characters language. Some of them are correctly
displayed while one doesn't. I've written :
-*- coding: utf-8 -*-

Is this wrong ?

Where can I find a pratical explanation about these encodings ?

Thanks.

You must know the encoding produced by your editor.

If you want to experiment, you could save a small file and run

od -Ax -tx1 thefile.py

to see exactly what codepoints appear. Compare those with the
charts at http://www.unicode.org/ and you can probably surmize
a good guess.


Perhaps faster, and certainly available on all platforms (unlike "od"):

Fire up Idle, then do this:
import sys
sys.stdout.encoding 'cp1252'
Why Idle, and not from the command line? Well because if you are on
Windows, you'll get a different answer, which is very unlikely to be the
one you want [unless you are using a legacy editor].

C:\junk>python
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information. import sys
sys.stdout.encoding 'cp850'

Aug 11 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.