about coding

John Machin sjmachin at lexicon.net
Thu Aug 11 17:58:36 EDT 2005


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'
 >>>



More information about the Python-list mailing list