SV: Unicode literals to latin-1

David.Reksten at sweco.no David.Reksten at sweco.no
Wed Jan 30 04:40:36 EST 2008


On 30. januar 2008 10:21, Berteun Damman wrote:
>On Wed, 30 Jan 2008 09:57:55 +0100, <David.Reksten at sweco.no>
><David.Reksten at 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')

I'm afraid that the string read from the database is a non-unicode string, thus me not using u"..." above. But it may contain unicode literals from the (Python-based) system that populated the table, and I'd like to get them back to proper unicode strings again, so that I can display them correctly to the user.

>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.

Any suggestions on how that would look, given the example above?

.david


More information about the Python-list mailing list