Python and unicode

Ben Finney ben+python at benfinney.id.au
Sun Sep 19 19:09:31 EDT 2010


Goran Novosel <goran.novosel at gmail.com> writes:

> # vim: set encoding=utf-8 :

This will help Vim, but won't help Python. Use the PEP 263 encoding
declaration <URL:http://www.python.org/dev/peps/pep-0263/> to let Python
know the encoding of the program source file.

    # -*- coding: utf-8 -*-

You can use the bottom of the file for editor hints.

> s='abcdef ČčĆćĐ𩹮ž'.decode('utf-8')
> ss=unicode('ab ČćŠđŽ','utf-8')

In Python 2.x, those string literals are created as byte strings, which
is why you're having to decode them. Instead, tell Python explicitly
that you want a string literal to be a Unicode text string:

    s = u'abcdef ČčĆćĐ𩹮ž'
    ss = u'ab ČćŠđŽ'

Learn more from the documentation <URL:http://docs.python.org/howto/unicode>.

-- 
 \      “He that would make his own liberty secure must guard even his |
  `\                             enemy from oppression.” —Thomas Paine |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list