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 Text', 7)
Then how can I get my orginal code back with codecs.unicode_escape_decode()
? I tried:
>>> codecs.unicode_escape_decode("(b'my Text', 7)")
("(b'my Text', 7)", 15)
but it does not give 'my text'
. If you need more details, please tell me.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With