Python Glossary Русский

Python 3.0+urllib.parsefunction

urllib.parse.unquote_to_bytes()

percent-encoding

Replaces %XX with raw bytes and returns bytes without decoding anything — for when the encoding is not known in advance.

Syntax

urllib.parse.unquote_to_bytes(string)

unquote() silently substitutes the replacement character for non-UTF-8 bytes, while this function returns them as they are, so you can choose the encoding yourself — for example cp1251 for URLs of old websites. It accepts str and bytes. It leaves '+' alone.

Examples

from urllib.parse import unquote, unquote_to_bytes
raw = unquote_to_bytes('%B8')
print(raw, raw.decode('cp1251'))  # → b'\xb8' ё
print(unquote('%B8') == '\ufffd')  # → True — байт потерян

See also

Open in the full glossarydocs.python.orgReport an inaccuracy