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
    "Fr\u00f8ya".decode('latin-1')
    but to no avail.

    ..david
    Guest, Jan 30, 2008
    #1
    1. Advertising

  2. On Wed, 30 Jan 2008 09:57:55 +0100, <>
    <> 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
    Berteun Damman, Jan 30, 2008
    #2
    1. Advertising

  3. >>>>> <> (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 <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
    Piet van Oostrum, Jan 30, 2008
    #3
  4. 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
    Marc 'BlackJack' Rintsch, Jan 30, 2008
    #4
  5. Guest

    Guest Guest

    SV: Unicode literals to latin-1

    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
    Guest, Jan 30, 2008
    #5
  6. On 30 ene, 07:54, <> 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
    Gabriel Genellina, Jan 30, 2008
    #6
  7. Guest

    Guest Guest

    SV: Unicode literals to latin-1

    On 30. januar 2008 14:31, Gabriel Genellina wrote:
    >On 30 ene, 07:54, <> 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
    Guest, Jan 30, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    870
    Roedy Green
    Nov 21, 2005
  2. John Goche
    Replies:
    8
    Views:
    16,424
  3. Replies:
    10
    Views:
    516
    Neil Cerutti
    Oct 19, 2006
  4. Simon Willison

    Treating a unicode string as latin-1

    Simon Willison, Jan 3, 2008, in forum: Python
    Replies:
    8
    Views:
    607
    Fredrik Lundh
    Jan 3, 2008
  5. Ioannis Vranos

    Non latin characters in string literals

    Ioannis Vranos, Jan 3, 2010, in forum: C Programming
    Replies:
    17
    Views:
    1,192
    Ben Bacarisse
    Jan 6, 2010
Loading...

Share This Page