Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread rajeev_jsv
On Wednesday 26 October 2005 2:57 am, Sundance wrote:
> I heard Sebastian Kügler said:
> > So what I basically need is a two-way conversion between unicode
> > strings and ascii (or at least something that is easily usable
> > together with psycopg and postgres' character fields.

from Unicode to 8Bit (UTF-8)
--
someStr = unicode(QString("hello"))
eightBitStr = someStr.encode('utf-8')


from 8Bit (UTF-8) top Unicode
--
read8Bit = 
unicodeStr = read8Bit.decode('utf-8')
qString = QString(unicodeStr)


eg. Pasted from Konsole
--
>>> smeStr = str('൱')
>>> smeStr.decode('utf-8')
u'\u0d71'


Rajeev J Sebastian

___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde


Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread Sebastian Kügler
On Tuesday 25 October 2005 23:27, Sundance wrote:
> You most probably want to read this:
> http://www.joelonsoftware.com/articles/Unicode.html

Thanks, it's a good read and filled me in on some wholes.

Cheers,
-- 
sebas

 http://www.kde.nl | http://vizZzion.org |  GPG Key ID: 9119 0EF9 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
I don't know what weapons World War Three will be fought with, but World War 
four will be fought with sticks and stones. -  Albert Einstein



pgp7s1PkYSWnb.pgp
Description: PGP signature
___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde


Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread Sundance
I heard Sebastian Kügler said:

> So what I basically need is a two-way conversion between unicode
> strings and ascii (or at least something that is easily usable
> together with psycopg and postgres' character fields.

Hello Sebastian,

You most probably want to read this:
http://www.joelonsoftware.com/articles/Unicode.html

Seriously. It's not a very long read, and you'll never have a problem 
with Unicode ever again.

And you'll get to appreciate how nice Python's Unicode support is, too. 
You'll see. It saved my day many times already. :)

Ta ta,

-- S.

___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde


Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread Sebastian Kügler
On Tuesday 25 October 2005 14:39, Diez B. Roggisch wrote:
> > Ah, that helps the problem in my test script. Now I'm encountering a
> > problem encoding the input of a QLineedit:
> >
> >   File "john.py", line 154, in dataFromGuiToUser
> > self.user.data["achternaam"] =
> > self.GAchternaam.text().encode('utf-8', 'strict')
> > AttributeError: encode
>
> text() returns a QString, which you should look up in the
> qt-reference-manual. That reveals the method
>
> QString::utf8()
>
> which returns a QCString. And that should be converted to a python byte
> string (by means of sip I think)

That doesn't seem to be the case, it's a QCString at that point.

> Then you need to _decode_ it as utf-8 to yield a unicode object. So the
> whole line should read
>
> self.user.data["achternaam"] =
> self.GAchternaam.text().utf8().decode('utf-8')

That throws me an AttributeError on encode().

However, if I use ascii(), it seems to work fine both ways:

self.GAchternaam.text().ascii()

Thanks for helping with that!
-- 
sebas

 http://www.kde.nl | http://vizZzion.org |  GPG Key ID: 9119 0EF9 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Natural selection won't matter soon, not anywhere as much as concious 
selection. We will civilize and alter ourselves to suit our ideas of what we 
can be.  Within one more human lifespan, we will have changed ourselves 
unrecognizably. - Greg Bear



pgpArEq2n2DT0.pgp
Description: PGP signature
___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde


Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread Giovanni Bajo
Sebastian Kügler <[EMAIL PROTECTED]> wrote:

>   File "john.py", line 154, in dataFromGuiToUser
> self.user.data["achternaam"] = self.GAchternaam.text().encode('utf-8',
> 'strict')
> AttributeError: encode
>
> So the problem is that I'm not able to convert the QString to utf-8
(without
> making it a str first, which bails out on UnicodeDecodeError. See attached
> snippet.
>
> Any idea?


Quickly skimming through QString docs reveals a utf8() method. Otherwise,
you can call unicode() just as you would call str(), and then using
unicode.encode.
-- 
Giovanni Bajo

___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde


Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread Hans-Peter Jansen
Am Dienstag, 25. Oktober 2005 12:48 schrieb Sebastian Kügler:
> On Tuesday 25 October 2005 12:28, Mateusz Korniak wrote:
> >
> > Encode to (and back from) utf-8.
> > Remember only that utf-8 representation is longer than number of
> > chars in unicode string if any non 7bit ascii char is used.
> > Also make sure that Yours DB iface allows to use 8bit encoding
> > (like iso-8859-1/2).
>
>   File "john.py", line 154, in dataFromGuiToUser
> self.user.data["achternaam"] =
> self.GAchternaam.text().encode('utf-8', 'strict')
> AttributeError: encode
>
> So the problem is that I'm not able to convert the QString to utf-8
> (without making it a str first, which bails out on
> UnicodeDecodeError. See attached snippet.

Consider using QString.utf8() method.

Pete

___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde


Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread Giovanni Bajo
Diez B. Roggisch <[EMAIL PROTECTED]> wrote:

> Then you need to _decode_ it as utf-8 to yield a unicode object. So the
> whole line should read
>
> self.user.data["achternaam"] =
> self.GAchternaam.text().utf8().decode('utf-8')


You're working way too hard to get an unicode string out of a QString:

self.user.data["achternaam"] = unicode(self.GAchternaam.text())

But Sebastian wanted a UTF-8 string, so calling .utf8() on the QString
should be sufficient.
-- 
Giovanni Bajo

___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde


Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread Diez B. Roggisch
> Ah, that helps the problem in my test script. Now I'm encountering a
> problem encoding the input of a QLineedit:
>
>   File "john.py", line 154, in dataFromGuiToUser
> self.user.data["achternaam"] = self.GAchternaam.text().encode('utf-8',
> 'strict')
> AttributeError: encode

text() returns a QString, which you should look up in the qt-reference-manual. 
That reveals the method

QString::utf8()

which returns a QCString. And that should be converted to a python byte string 
(by means of sip I think)

Then you need to _decode_ it as utf-8 to yield a unicode object. So the whole 
line should read

self.user.data["achternaam"] = self.GAchternaam.text().utf8().decode('utf-8')

Regards,

Diez

___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde


Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread Sebastian Kügler
On Tuesday 25 October 2005 12:28, Mateusz Korniak wrote:
> > When I read the data from the widget, I'm not able to insert strings into
> > the database using unicode. I can replace the offending characters with
> > 'string'.encode('ascii', 'xmlcharrefreplace').
>
> Encode to (and back from) utf-8.
> Remember only that utf-8 representation is longer than number of chars in
> unicode string if any non 7bit ascii char is used.
> Also make sure that Yours DB iface allows to use 8bit encoding (like
> iso-8859-1/2).

Ah, that helps the problem in my test script. Now I'm encountering a problem 
encoding the input of a QLineedit:

  File "john.py", line 154, in dataFromGuiToUser
self.user.data["achternaam"] = self.GAchternaam.text().encode('utf-8', 
'strict')
AttributeError: encode

So the problem is that I'm not able to convert the QString to utf-8 (without 
making it a str first, which bails out on UnicodeDecodeError. See attached 
snippet.

Any idea?
-- 
sebas

 http://www.kde.nl | http://vizZzion.org |  GPG Key ID: 9119 0EF9 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
A day without sunshine is like a day without orange juice.



unicode-test.py
Description: application/python


pgpdx9YJ23sYh.pgp
Description: PGP signature
___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde


Re: [PyKDE] unicode two-way conversion

2005-10-25 Thread Mateusz Korniak
On Tuesday 25 of October 2005 12:13, Sebastian Kügler wrote:
> Hi,
>
> (I'm posting to this list since I think it's likely that someone here ran
> across the same problem.)
>
> I'm working on a database frontend for a postgresql database, and I'm
> having an issue with unicode characters that I've put into the Qt UI.
>
> When I read the data from the widget, I'm not able to insert strings into
> the database using unicode. I can replace the offending characters with
> 'string'.encode('ascii', 'xmlcharrefreplace').

Encode to (and back from) utf-8.
Remember only that utf-8 representation is longer than number of chars in 
unicode string if any non 7bit ascii char is used.
Also make sure that Yours DB iface allows to use 8bit encoding (like 
iso-8859-1/2).

> So what I basically need is a two-way conversion between unicode strings
> and ascii (or at least something that is easily usable together with
> psycopg and postgres' character fields.
>
> Attached scriptlet should show that problem.

Another solution (and prolly better) to You, may be to create unicode 
Postgresql database and do not care about encoding anymore at all.

-- 
Mateusz Korniak
"Black holes are where God divided by zero." - Steven Wright

___
PyKDE mailing listPyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde