Python and encodings drives me crazy

Konstantin Veretennicov kveretennicov at gmail.com
Mon Jun 20 18:13:53 EDT 2005


On 6/20/05, Oliver Andrich <oliver.andrich at gmail.com> wrote:
> Does the following code write headline and caption in
> MacRoman encoding to the disk?
> 
>     f = codecs.open(outfilename, "w", "macroman")
>     f.write(headline)

It does, as long as headline and caption *can* actually be encoded as
macroman. After you decode headline from utf-8 it will be unicode and
not all unicode characters can be mapped to macroman:

>>> u'\u0160'.encode('utf8')
'\xc5\xa0'
>>> u'\u0160'.encode('latin2')
'\xa9'
>>> u'\u0160'.encode('macroman')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "D:\python\2.4\lib\encodings\mac_roman.py", line 18, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u0160' in position
 0: character maps to <undefined>

- kv



More information about the Python-list mailing list