[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Unicode literals to latin-1

David.Reksten

1/30/2008 8:58:00 AM

How can I convert a string read from a database containing unicode literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?

I have tried variations around
"Fr\u00f8ya".decode('latin-1')
but to no avail.

..david
6 Answers

Berteun Damman

1/30/2008 9:21:00 AM

0

On Wed, 30 Jan 2008 09:57:55 +0100, <David.Reksten@sweco.no>
<David.Reksten@sweco.no> wrote:
> How can I convert a string read from a database containing unicode
> literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Fr�¸ya"?
>
> I have tried variations around
> "Fr\u00f8ya".decode('latin-1')
> but to no avail.

Assuming you use Unicode-strings, the following should work:
u"Fr\u00f8ya".encode('latin-1')

That is, for some string s, s.decode('encoding') converts the
non-unicode string s with encoding to a unicode string u. Whereas
for some unicode string u, u.encode('encoding') converts the unicode
string u into a non-unicode string with the specified encoding.

You can use s.encode() on a non-unicode string, but it will first try to
decode it (which might give an DecodeError if there are non-ASCII
characters present) and it will then encode it.

Berteun

Piet van Oostrum

1/30/2008 9:22:00 AM

0

>>>>> <David.Reksten@sweco.no> (DR) wrote:

>DR> How can I convert a string read from a database containing unicode literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?
>DR> I have tried variations around
>DR> "Fr\u00f8ya".decode('latin-1')
>DR> but to no avail.

You have to use encode instead of decode, and the input string must be a
unicode string.

>>> print u"Fr\u00f8ya".encode('latin-1')
Frøya
>>>


--
Piet van Oostrum <piet@cs.uu.nl>
URL: http://pietvano... [PGP 8DAE142BE17999C4]
Private email: piet@vanoostrum.org

Marc 'BlackJack' Rintsch

1/30/2008 9:48:00 AM

0

On Wed, 30 Jan 2008 09:57:55 +0100, David.Reksten wrote:

> How can I convert a string read from a database containing unicode
> literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?
>
> I have tried variations around
> "Fr\u00f8ya".decode('latin-1')
> but to no avail.

In [388]: 'Fr\u00f8ya'.decode('unicode-escape')
Out[388]: u'Fr\xf8ya'

In [389]: print 'Fr\u00f8ya'.decode('unicode-escape')
Frøya

Ciao,
Marc 'BlackJack' Rintsch

David.Reksten

1/30/2008 9:54:00 AM

0

On 30. januar 2008 10:48, Marc 'BlackJack' Rintsch wrote:
>On Wed, 30 Jan 2008 09:57:55 +0100, David.Reksten wrote:
>
>> How can I convert a string read from a database containing unicode
>> literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?
>>
>> I have tried variations around
>> "Fr\u00f8ya".decode('latin-1')
>> but to no avail.
>
>In [388]: 'Fr\u00f8ya'.decode('unicode-escape')
>Out[388]: u'Fr\xf8ya'
>
>In [389]: print 'Fr\u00f8ya'.decode('unicode-escape')
>Frøya

'unicode-escape' did the trick! Thank you!

..david

Gabriel Genellina

1/30/2008 1:31:00 PM

0

On 30 ene, 07:54, <David.Reks...@sweco.no> wrote:
> On 30. januar 2008 10:48, Marc 'BlackJack' Rintsch wrote:
>
> >On Wed, 30 Jan 2008 09:57:55 +0100, David.Reksten wrote:
>
> >> How can I convert a string read from a database containing unicode
> >> literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?

> >In [388]: 'Fr\u00f8ya'.decode('unicode-escape')
> >Out[388]: u'Fr\xf8ya'
>
> 'unicode-escape' did the trick! Thank you!

A unicode-escaped string looks very strange in a database... I'd
revise the way things are stored and retrieved.

--
Gabriel Genellina

David.Reksten

1/30/2008 2:48:00 PM

0

On 30. januar 2008 14:31, Gabriel Genellina wrote:
>On 30 ene, 07:54, <David.Reks...@sweco.no> wrote:
>> On 30. januar 2008 10:48, Marc 'BlackJack' Rintsch wrote:
>>
>> >On Wed, 30 Jan 2008 09:57:55 +0100, David.Reksten wrote:
>>
>> >> How can I convert a string read from a database containing unicode
>> >> literals, such as "Fr\u00f8ya" to the latin-1 equivalent, "Frøya"?
>
>> >In [388]: 'Fr\u00f8ya'.decode('unicode-escape')
>> >Out[388]: u'Fr\xf8ya'
>>
>> 'unicode-escape' did the trick! Thank you!
>
>A unicode-escaped string looks very strange in a database... I'd
>revise the way things are stored and retrieved.

I agree. I'm currently using the trick above to fix it.

..david