0

How do I convert a string that looks like the following:

s1 = u'MDcxNTFjZWU5MzQ2MTRjZmZiOWIyNTBhYjJlZDhkODY0OTEyYmE2Yjp7ImFjdHVhbF9jcmVhdGVkX3RpbWVzdGFtcCI6ICIxNjcyNDg5NjAxLjMyOTg5MyIsICJhY3R1YWxfaWQiOiAiYWhGa1pYWi1ibWxuYUhSc2IyOXdMVzVsZDNJakN4SWJibWxuYUhSc2IyOXdYMUpsYzJWeWRtRjBhVzl1UVdOMGRXRnNHTUc3QVF3In0%3D'

to a string that looks like this

s2 = u'MDcxNTFjZWU5MzQ2MTRjZmZiOWIyNTBhYjJlZDhkODY0OTEyYmE2Yjp7ImFjdHVhbF9jcmVhdGVkX3RpbWVzdGFtcCI6ICIxNjcyNDg5NjAxLjMyOTg5MyIsICJhY3R1YWxfaWQiOiAiYWhGa1pYWi1ibWxuYUhSc2IyOXdMVzVsZDNJakN4SWJibWxuYUhSc2IyOXdYMUpsYzJWeWRtRjBhVzl1UVdOMGRXRnNHTUc3QVF3In0='

Notice that s1 ends with %3D which is the unicode representation of =

I have tried using the .decode function but that doesn't seem to work.

1
  • %3D is not the unicode representation of =. That is called url escaping. Commented Dec 31, 2022 at 17:12

1 Answer 1

1

This isn't about Unicode encoding (a "Unicode string" is the same thing as a "native string" in modern Python; what you're thinking of is bytes vs str), it's about URL encoding. Use urllib.parse.unquote:

>>> import urllib.parse
>>> urllib.parse.unquote('MDcxNTFjZWU5MzQ2MTRjZmZiOWIyNTBhYjJlZDhkODY0OTEyYmE2Yjp7ImFjdHVhbF9jcmVhdGVkX3RpbWVzdGFtcCI6ICIxNjcyNDg5NjAxLjMyOTg5MyIsICJhY3R1YWxfaWQiOiAiYWhGa1pYWi1ibWxuYUhSc2IyOXdMVzVsZDNJakN4SWJibWxuYUhSc2IyOXdYMUpsYzJWeWRtRjBhVzl1UVdOMGRXRnNHTUc3QVF3In0%3D')
'MDcxNTFjZWU5MzQ2MTRjZmZiOWIyNTBhYjJlZDhkODY0OTEyYmE2Yjp7ImFjdHVhbF9jcmVhdGVkX3RpbWVzdGFtcCI6ICIxNjcyNDg5NjAxLjMyOTg5MyIsICJhY3R1YWxfaWQiOiAiYWhGa1pYWi1ibWxuYUhSc2IyOXdMVzVsZDNJakN4SWJibWxuYUhSc2IyOXdYMUpsYzJWeWRtRjBhVzl1UVdOMGRXRnNHTUc3QVF3In0='
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.