unicode and os.system

D

Dumbkiwi

I've got a rather large python script that I write and maintain. It has
some interaction with other programmes on the linux/kde desktop through
the dcop interface. This script also uses the gettext module to enable
the output of the script to be translated into several languages,
including utf-8 encoded text.

However, when I issue a dcop call to an application (which is a docker
application that can display text above an icon), the operation fails with:

Traceback (most recent call last):
File "/home/matt/karamba/lwbkup/liquid_weather.py", line 2970, in widgetUpdated
os.system('dcop kxdocker docker changeTextByName Current "%s : %s"' %(_(situtext), weather.temperature()))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 2:
ordinal not in range(128)

_(situtext) is a variable containing a unicode string.

My python environment has ascii as its encoding for stdoutANSI_X3.4-1968

the dcop call I'm using requires a QString variable where the _(situtext)
is.

Can anyone help me to work through this issue? I'm a bit lost as to where
to start.

Thanks

Matt
 
E

Erik Max Francis

Dumbkiwi said:
Can anyone help me to work through this issue? I'm a bit lost as to where
to start.

If you want to convert it to UTF-8, then do so with

u.decode('utf-8')
 
D

Dumbkiwi

If you want to convert it to UTF-8, then do so with

u.decode('utf-8')

I've tried that previously, and get:

Traceback (most recent call last):
File "/home/matt/karamba/lwbkup/liquid_weather.py", line 2765, in initWidget
os.system('dcop kxdocker docker addIcon Current %s "%s : %s" /dev/null GIcon lwp none none none none' %(icopath, QString(situtext.decode('utf-8'), weather.temperature()))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in
position 9: ordinal not in range(128)

Thanks for responding though.

Matt
 
E

Erik Max Francis

Dumbkiwi said:
I've tried that previously, and get:

Traceback (most recent call last):
File "/home/matt/karamba/lwbkup/liquid_weather.py", line 2765, in initWidget
os.system('dcop kxdocker docker addIcon Current %s "%s : %s" /dev/null GIcon lwp none none none none' %(icopath, QString(situtext.decode('utf-8'), weather.temperature()))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in
position 9: ordinal not in range(128)

Thanks for responding though.

That means one of the other things you're trying to print with %s is
also a Unicode string, and it contains non-ASCII characters. Try
printing their types to see which one it is.
 
J

jepler

I think you need
u.encode('utf-8')
..encode() turns unicode into a byte string, .decode() turns a byte string into
unicode.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFDGRR8Jd01MZaTXX0RAgNIAJ4/h8DGTKhMn7yNpSFz9CistqflAQCeKhIb
gXF+JxnGfkGohQr6PRzotmo=
=KCun
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top