By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

unicode and os.system

P: n/a
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 stdout
import sys
print sys.stdout.encoding

ANSI_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
Sep 2 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Dumbkiwi wrote:
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')

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Liberty is the right to do whatever the law permits.
-- Charles Louis Montesquieu
Sep 2 '05 #2

P: n/a
On Fri, 02 Sep 2005 16:11:48 -0700, Erik Max Francis wrote:
Dumbkiwi wrote:
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')


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
Sep 2 '05 #3

P: n/a
Dumbkiwi wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Liberty is the right to do whatever the law permits.
-- Charles Louis Montesquieu
Sep 2 '05 #4

P: n/a
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-----

Sep 3 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.