kdialog and unicode

John Ridley ojokimu at yahoo.co.uk
Wed Apr 27 20:28:52 EDT 2005


dmbkiwi 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')
> 
> 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?

kdialog will accept a limited form of markup as text, including xml
character escapes. so you should be able to display polish text
correctly using this method:

>>> import os
>>> s = """Wszyscy ludzie rodz\xc4\x85 si\xc4\x99
...  wolni i r\xc3\xb3wni pod wzgl\xc4\x99dem
...  swej godno\xc5\x9bci i swych praw. S\xc4\x85
...  oni obdarzeni rozumem i sumieniem i powinni
...  post\xc4\x99powa\xc4\x87 wobec innych w duchu
...  braterstwa."""
>>> s = s.decode('utf-8').encode('ascii','xmlcharrefreplace')
>>> k = os.popen('kdialog --inputbox "<p>%s</p>"' % s)


John Ridley

Send instant messages to your online friends http://uk.messenger.yahoo.com 



More information about the Python-list mailing list