conversion to and from unicode strings

Discussion in 'Python' started by abhishek, Oct 27, 2008.

  1. abhishek

    abhishek Guest

    hello group,
    i want to represent and store a string u'\x00\x07\xa7' as
    '\x00\x07\xa7'. any ideas on how to achieve this.
    abhishek, Oct 27, 2008
    #1
    1. Advertising

  2. abhishek wrote:
    > hello group,
    > i want to represent and store a string u'\x00\x07\xa7' as
    > '\x00\x07\xa7'. any ideas on how to achieve this.


    You want to store it in the form of the repr() of the string? It is
    possible to use repr() to get a bytestring to store and to use eval() to
    create a unicode string of that bytestring again. But that's just bad.

    It's much better to use a encoding that can represent all Unicode
    characters like UTF-8.

    >>> s = u'\x00\x07\xa7'
    >>> bytestr = s.encode("utf-8")
    >>> bytestr

    '\x00\x07\xc2\xa7'
    >>> out_str = unicode(bytestr, "utf-8")
    >>> out_str == s

    True

    -- Gerhard
    Gerhard Häring, Oct 27, 2008
    #2
    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. Holger Joukl
    Replies:
    5
    Views:
    503
    Ben Finney
    Dec 13, 2006
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    723
    Malcolm
    Jun 24, 2006
  3. Nagarajan
    Replies:
    4
    Views:
    295
    Nagarajan
    Aug 23, 2007
  4. Asterix
    Replies:
    5
    Views:
    690
    Matt Nordhoff
    Aug 31, 2008
  5. fhscobey
    Replies:
    0
    Views:
    225
    fhscobey
    Nov 3, 2005
Loading...

Share This Page