unicode and os.system

2005-09-02 Thread 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 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode and os.system

2005-09-02 Thread Dumbkiwi
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
-- 
http://mail.python.org/mailman/listinfo/python-list


kdialog and unicode

2005-04-26 Thread Dumbkiwi
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 "", 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?

Or have I completely misunderstood the whole process, in which case, can
you please enlighten me.

Matt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: kdialog and unicode

2005-04-26 Thread Dumbkiwi
On Tue, 26 Apr 2005 11:41:01 +0200, Peter Otten wrote:

> 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 "", 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

I had tried that, but then the text looks like crap.  The text I'm using
for this is Polish, and there are a lot of non-English characters in
there. Using this method results in some strange characters - basically it
looks like a file encoded in utf-8, but displayed using iso-8859-1.

Is this the best I can do?

Thanks for your help.

Matt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: kdialog and unicode

2005-04-26 Thread dumbkiwi
Peter Otten <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> Dumbkiwi wrote:
> 
> >> Just encode the data in the target encoding before passing it to
> >> os.popen():
> >> 
> >> test = os.popen('kdialog --inputbox %s' % data.encode("utf-8"))
>  
> > I had tried that, but then the text looks like crap.  The text I'm using
> > for this is Polish, and there are a lot of non-English characters in
> > there. Using this method results in some strange characters - basically it
> > looks like a file encoded in utf-8, but displayed using iso-8859-1.
> > 
> > Is this the best I can do?
> 
> I've just tried the setup you described (with German umlauts instead of
> Polish characters) on my Suse 9.1, and it works as expected with both
> Python 2.3 and 2.4. Perhaps the target encoding you need is not UTF-8. I
> would try other popular encodings used for Polish text (no idea what these
> are). sys.stdout.encoding might give you a clue.
> 
> Peter

Both sys.stdout.encoding and sys.stdin.encoding give:

ANSI_X3.4-1968

which is ascii (I think).

I'd be interested to see what your default encoding is, and why your
output was different.

Anyway, from your post, I've done some more digging, and found the
command:

sys.setappdefaultencoding()

which I've used, and it's fixed the problem (I think).

Thanks for your help.

Matt
-- 
http://mail.python.org/mailman/listinfo/python-list


Convert xml symbol notation

2007-04-06 Thread dumbkiwi
Hi,

I'm working on a script to download and parse a web page, and it
includes xml symbol notation, such as ' for the ' character.  Does
anyone know of a pre-existing python script/lib to convert the xml
notation back to the actual symbol it represents?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert xml symbol notation

2007-04-07 Thread dumbkiwi
On Apr 7, 5:23 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> dumbkiwi wrote:
> > I'm working on a script to download and parse a web page, and it
> > includes xml symbol notation, such as ' for the ' character.  Does
> > anyone know of a pre-existing python script/lib to convert the xml
> > notation back to the actual symbol it represents?
>
> Try the htmlentitydefs module.

Is that a standard module?  I can't see it anywhere - googled it.


-- 
http://mail.python.org/mailman/listinfo/python-list


urllib2 hangs "forever" where there is no network interface

2007-01-31 Thread dumbkiwi
I have written a script that uses the urllib2 module to download web
pages for parsing.

If there is no network interface, urllib2 hangs for a very long time
before it raises an exception.  I have set the socket timeout with
socket.setdefaulttimeout(), however, where there is no network
interface, this seems to be ignored - presumably, because without a
network interface, there is nothing for the socket module to interact
with.

So, can someone point me in the right direction, so that I can catch
an exception where there is no network interface?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: urllib2 hangs "forever" where there is no network interface

2007-02-01 Thread dumbkiwi
On Feb 2, 5:02 am, [EMAIL PROTECTED] (John J. Lee) wrote:
> "dumbkiwi" <[EMAIL PROTECTED]> writes:
> > I have written a script that uses the urllib2 module to download web
> > pages for parsing.
>
> > If there is no network interface, urllib2 hangs for a very long time
> > before it raises an exception.  I have set the socket timeout with
> > socket.setdefaulttimeout(), however, where there is no network
> > interface, this seems to be ignored - presumably, because without a
> > network interface, there is nothing for the socket module to interact
> > with.
>
> > So, can someone point me in the right direction, so that I can catch
> > an exception where there is no network interface?
>
> Are you on Windows or something Unixy?

Linux
>
> Presumably Windows?  (Unix systems almost always have at least a
> loopback interface)
>
> John

Sorry, I should have been more specific.  The network interfaces are
up - ie lo and eth1, it's where the wireless connection has dropped
out.  Is the best solution to test for a wireless connection through /
proc before trying to download data?

-- 
http://mail.python.org/mailman/listinfo/python-list