Python-encoding And Decoding Using Codecs,unicode_escape()
I am trying to encode and decode a text in python using the codecs library. Here is my code: >>> import codecs >>> codecs.unicode_escape_encode('my Text') (b'my T
Solution 1:
I think you are pasting the wrong thing back to the function. Correct usage would be:
>>>import codecs>>>codecs.unicode_escape_encode('my Text')
(b'my Text', 7)
>>>codecs.unicode_escape_decode(b'my Text')
('my Text', 7)
Actually a more relevant example would be:
>>> codecs.unicode_escape_encode('Hëllö')
(b'H\\xebll\\xf6', 5)
>>> codecs.unicode_escape_decode(b'H\\xebll\\xf6')
('Hëllö', 11)
The "normal" letters are 1:1 the same, both in the encoded and in the decoded versions. The "special" letters sometimes take more than one byte and as such are represented in encoded format with their hex numbers eg \\xeb
represents the ë
in encoded form.
More info here: https://en.wikipedia.org/wiki/UTF-8
Post a Comment for "Python-encoding And Decoding Using Codecs,unicode_escape()"