Url Decode UTF-8 in Python
Problem: Given a URL encoded in UTF-8 format, how can it be decoded to its intended string representation in Python 2.7?
Solution:
The problem stems from the presence of UTF-8 encoded bytes that are escaped with URL quoting. To correctly decode this data, a two-step process is required:
<code class="python">from urllib.parse import unquote url = 'example.com?title=%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D0%B2%D0%B0%D1%8F+%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B0' decoded_url = unquote(url) print(decoded_url) # Output: example.com?title=правовая+защита</code>
This approach seamlessly handles the decoding from percent-encoded data to UTF-8 bytes and finally to text.
The above is the detailed content of How to Decode UTF-8 URL Encoded Strings in Python 2.7?. For more information, please follow other related articles on the PHP Chinese website!