Unicode literals to latin-1

Discussion in 'Python' started by Guest, Jan 30, 2008.

  1. Guest

    Guest 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
    but to no avail.

    Guest, Jan 30, 2008
    1. Advertisements

  2. Assuming you use Unicode-strings, the following should work:

    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 Damman, Jan 30, 2008
    1. Advertisements

  3. You have to use encode instead of decode, and the input string must be a
    unicode string.
    Piet van Oostrum, Jan 30, 2008
  4. In [388]: 'Fr\u00f8ya'.decode('unicode-escape')
    Out[388]: u'Fr\xf8ya'

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

    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Jan 30, 2008
  5. Guest

    Guest Guest

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

    Guest, Jan 30, 2008
  6. A unicode-escaped string looks very strange in a database... I'd
    revise the way things are stored and retrieved.
    Gabriel Genellina, Jan 30, 2008
  7. Guest

    Guest Guest

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

    Guest, Jan 30, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.