Unicode literals to latin-1


G

Guest

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
 
Ad

Advertisements

B

Berteun Damman

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
 
P

Piet van Oostrum

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

Marc 'BlackJack' Rintsch

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
 
G

Guest

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
 
G

Gabriel Genellina

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

Advertisements

G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top