[PyQt] unicode and pyqt4

Tobias Rzepka tobias.rzepka at googlemail.com
Wed Nov 24 19:51:40 GMT 2010


Janwillem van Dijk schrieb am 24.11.2010 17:40:
> On 11/24/2010 10:04 AM, Yao Ko wrote:
>> On Wed, Nov 24, 2010 at 12:58 AM, Janwillem van Dijk
>> <jwevandijk at xs4all.nl>  wrote:
>>> I have a small application that displays a tree structure using ete2
>>> (http://ete.cgenomics.org/). The node names are unicode and contain in
>>> particular German and French special chars. When I run the script 
>>> from Eric4
>>> or directly from a terminal with  "python scriptname.py" all is OK. 
>>> However
>>> I want to run it as a subprocess and than the special chars generate 
>>> errors:
>>>
>>> import subprocess
>>> arg =['python', script_path_name]
>>> p = subprocess.Popen(arg)
>> What's the error output?
>>
>> Yao
>>
>>
> The traceback.print_exc gives:
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in 
> position 1: ordinal not in range(128)
> print u'\xe4' correctly shows the German a-umlaut
>
> I made a small PyQt4 test prog with a painTextEdit that shows the 
> character correctly, both when started directly and as a subprocess. 
> Sometimes I think I understood the in and outs of unicode and 
> sometimes I think I am getting mad of unicode and (Qt) python.
>
> Any suggestions where to look?
> Thanks, Janwillem
>
Hello Janwillem,

you have to convert the strings:

import codecs
codecs.encode(u'äöü', 'iso8859-1')  # u'a-umlaut o-umlaut u-umlaut' or 
u'\xe4\xf6\xfc'

If you get unicode text from PyQt and want to use it on the command line 
or write it down, you have to convert it. Another example is
fp = open('test.txt', 'wb')
fp.write(u'\xe4\xf6\xfc')    # Throws exception
fp.write(codecs.encode(u'\xe4\xf6\xfc', 'iso8859-1'))    # ok
fp.close()

Regards, Tobias


More information about the PyQt mailing list