Reputation: 5478
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?
Upvotes: 3
Views: 7136
Reputation: 11885
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'
Upvotes: 4
Reputation: 12772
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
Upvotes: 7