Python Glossary Русский

Python 3.0+urllib.parsefunction

urllib.parse.quote_from_bytes()

percent-encoding

Like quote(), but takes bytes: every byte outside the safe set is encoded as %XX.

Syntax

urllib.parse.quote_from_bytes(bytes, safe='/')

Useful when the data is already bytes in the required encoding, for example cp1251 for a legacy server: quote() would convert a str to UTF-8 itself. It does not accept a str — that is a TypeError. As with quote(), the default is safe='/', so the slash is left alone.

Examples

from urllib.parse import quote_from_bytes
print(quote_from_bytes('ё'.encode('cp1251')))  # → %B8
print(quote_from_bytes(b'\xff /'))  # → %FF%20/

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy