| FAQ Index - Search - Recent Changes - Everything - Add entry |
4.10. How can I use Unicode (or other format) strings in my PyGTK application, or, why do I get a UTF-8 warning when using a string?
If your string is a Python unicode (u"some string") object, you needn't do anything else, PyGTK will happily use it.
If, however, your string is an str ("some string") object, PyGTK requires it to be in UTF-8. Otherwise you will see a warning like this:
WARNING **: Invalid UTF8 string passed to pango_layout_set_text()and your text will be displayed incorrectly.
Luckily, fixing this is trivial in Python by converting the str into a unicode object:
unistr = unicode('your iso 8859-15 text', 'iso8859-15')
# or: 'your iso 8859-15 text'.decode('iso8859-15')
label = gtk.Label(unistr)
If you have no clue about the character encoding used, you can still convert it while silently ignoring errors:
unistr = unicode('text with an unknown encoding', errors='replace')
This will replace any unknown character by the official Unicode replacement character (U+FFFD).
