By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,592 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,592 IT Pros & Developers. It's quick & easy.

conversion to and from unicode strings

P: n/a
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.
Oct 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Oct 27 '08 #2

P: n/a
abhishek <gu***************@gmail.comwrote:
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 can use latin-1 encoding.
>>u = ''.join(unichr(c) for c in range(256))
[ord(c) for c in u.encode('latin1')] == range(256)
True
--
Duncan Booth http://kupuguy.blogspot.com
Oct 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.