[PyKDE] qstring(qcstring) into python string

Gerard Vermeulen gvermeul at grenoble.cnrs.fr
Thu Apr 24 19:30:00 BST 2003


On Thu, 24 Apr 2003 17:47:29 +0200
Petr Vanìk <subzero at py.cz> wrote:

> hello.
> 
> how do you convert the qstring into python one?
> str(qstring) works fine just for ascii/latin1
> something like
> ret = unicode(str(qstring.local8Bit()), "utf-8")
> raises 
> UnicodeError: UTF-8 decoding error: unexpected code byte
> 
> headache today :-/
> 
If you have installed Eric, there is a file site-customize.py:
[packer at venus python2.2]$ less /usr/lib/python2.2/site-packages/sitecustomize.py

#
# sitecustomize.py - saving a useful function.
# generated by the eric install script
#

import sys

sys.setappdefaultencoding = sys.setdefaultencoding

# end of file

So, from Python you can do:

[packer at venus python2.2]$ python
Python 2.2.2 (#1, Oct 23 2002, 12:12:09)
[GCC 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)] on linux-i386
Type "help", "copyright", "credits" or "license" for more information.
>>> from qt import *; import sys
>>> str(QString(u'\u0411\u0412'))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>> sys.setappdefaultencoding('utf-8')
>>> str(QString(u'\u0411\u0412'))
'\xd0\x91\xd0\x92'
>>> unicode(QString(u'\u0411\u0412'))
u'\u0411\u0412'
>>>

Hope this makes your headache disappear,

Gerard




More information about the PyQt mailing list