4

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.

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.