I need to convert a cyrillic string to its urlencoded version in Windows-1251 encoding. For the following example string:
Моцарт
The correct result should be:
%CC%EE%F6%E0%F0%F2
In PHP, I would simply do the following:
$request = urlencode(iconv("UTF-8", "windows-1251", "Моцарт"));
echo $request;
How to accomplish the same goal in Python?
Use the decode and encode method on string, then use urllib.quote
import urllib
print urllib.quote(s.decode('utf8').encode('cp1251'))
prints
%CC%EE%F6%E0%F0%F2
In Python 3, use the quote() function found in urllib.request:
from urllib import request
request.quote("Моцарт".encode('cp1251'))
# '%CC%EE%F6%E0%F0%F2'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With