kdialog and unicode

Peter Otten __peter__ at web.de
Tue Apr 26 05:41:01 EDT 2005


Dumbkiwi wrote:

> I'm trying to get python, unicode and kdialog to play nicely together.
> This is a linux machine, and kdialog is a way to generate dialog boxes in
> kde with which users can interact (for example input text), and you can
> use the outputted text in your script.
> 
> Anyway, what I'm doing is reading from a utf-8 encoded text file using the
> codecs module, and using the following:
> 
> data = codecs.open('file', 'r', 'utf-8')

data is now a unicode string.

> 
> I then manipulate the data to break it down into text snippets.
> 
> Then I run this command:
> 
>>>> test = os.popen('kdialog --inputbox %s' %(data))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u017a' in
> position 272: ordinal not in range(128)
> 
> I would really like kdialog display the text as utf-8.  However, it seems
> that python is trying to pass the utf-8 encoded data as ascii, which
> obviously fails because it can't deal with the utf-8 encoded text.  Is it
> possible to pass the text out to kdialog as utf-8, rather than ascii?

Just encode the data in the target encoding before passing it to os.popen():

test = os.popen('kdialog --inputbox %s' % data.encode("utf-8"))

Peter




More information about the Python-list mailing list