John Ridley wrote: > 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
Interesting - this displays correctly when I run the above code from a python shell. However, when I run it as a superkaramba theme (which is a wrapper interface to some kde functions, but allegedly passes straight python functions direct to the python interpreter), it shows up as letters, with &nnn (where nnn is a number) instead of the \xc4 etc. I need to do some more investigating of this with the superkaramba developers, and my own machine to see where the problem lies - clearly it's not in the code I'm using - there's either something wrong on my machine, or wrong with the way superkaramba passes this code to the python interpreter. Thanks for your help. Matt -- http://mail.python.org/mailman/listinfo/python-list