[英]Urlencode cyrillic characters in Python
I need to convert a cyrillic string to its urlencoded version in Windows-1251 encoding. 我需要将西里尔字符串转换为Windows-1251编码的urlencoded版本。 For the following example string: 对于以下示例字符串:
Моцарт Моцарт
The correct result should be: 正确的结果应该是:
%CC%EE%F6%E0%F0%F2 %CC%EE%F6%E0%F0%F2
In PHP, I would simply do the following: 在PHP中,我只需执行以下操作:
$request = urlencode(iconv("UTF-8", "windows-1251", "Моцарт"));
echo $request;
How to accomplish the same goal in Python? 如何在Python中实现相同的目标?
Use the decode
and encode
method on string, then use urllib.quote
在string上使用decode
和encode
方法,然后使用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
: 在Python 3中,使用urllib.request
的quote()
函数:
from urllib import request
request.quote("Моцарт".encode('cp1251'))
# '%CC%EE%F6%E0%F0%F2'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.