I want to represent all characters in a string as in this table.
But when I do
raw = 'æøå'
encoded = raw.encode('cp1252')
print(encoded)
I get
>>> b'\xe6\xf8\xe5'
What I want is
>>> %E6%F8%E5
as a string for use in a URL.
I want to represent all characters in a string as in this table.
But when I do
raw = 'æøå'
encoded = raw.encode('cp1252')
print(encoded)
I get
>>> b'\xe6\xf8\xe5'
What I want is
>>> %E6%F8%E5
as a string for use in a URL.
You have to "quote" your string using urllib tools.
import urllib.parse
raw = 'æøå'
print(urllib.parse.quote(raw, encoding='cp1252'))
# returns "%E6%F8%E5"
urlib.parse.quote() takes an encoding argument directly.urllib.parse.quote(raw, encoding='cp1252'), skip the raw.encode() call altogether.bytes, at which point I'd recommend you use urllib.parse.quote_from_bytes(), to be explicit about what is being done. Also, the OP probably wants to use quote_plus(), not quote(), as the vast majority of these want the application/x-www-form-urlencoded content type variant.
urllib.parse.quote()takes care of encoding for you.