Python编码空间的编码不正确
我有一本字典如下Python编码空间的编码不正确,python,encoding,urlencode,Python,Encoding,Urlencode,我有一本字典如下 params = { 'response_type': 'token', 'client_id': o_auth_client_id, 'redirect_url': call_back_url, 'scope': 'activity heartrate location' } print urllib.urlencode(params) 嗯,对它进行编码 但结果是 重定向\u url=http%3A%2F%2
params = {
'response_type': 'token',
'client_id': o_auth_client_id,
'redirect_url': call_back_url,
'scope': 'activity heartrate location'
}
print urllib.urlencode(params)
嗯,对它进行编码
但结果是
重定向\u url=http%3A%2F%2F127.0.0.1%3A8084%2Agile\u healtg%2Access\u令牌%2F和响应\u类型=令牌和客户端\u id=xxxxxx&scope=activity+heartrate+location
嗯,得到了上面的东西
不幸的是,空格被编码为+符号
但结果应该是肯定的
范围=活动%20营养%20心率
如何在python中实现对空格的正确编码?请查看文档中的相关信息
quote_plus
方法用于在传递键值时将空格更改为加号。您可以使用unquote\u plus
方法删除加号,然后quote
以所需格式对其进行编码
您基本上需要对参数使用
quote
方法检查文档中的方法
quote_plus
方法用于在传递键值时将空格更改为加号。您可以使用unquote\u plus
方法删除加号,然后quote
以所需格式对其进行编码
您基本上需要对参数使用
quote
方法此程序可能会满足您的要求
import urllib
def my_special_urlencode(params):
return '&'.join('{}={}'.format(urllib.quote(k, ''), urllib.quote(v, '')) for k,v in params.items())
params = {
'response_type': 'token',
'client_id': 'xxxxxx',
'redirect_url': 'http://example.com/callback',
'scope': 'activity heartrate location'
}
print my_special_urlencode(params)
结果:
redirect_url=http%3A%2F%2Fexample.com%2Fcallback&response_type=token&client_id=xxxxxx&scope=activity%20heartrate%20location
此程序可能会满足您的要求
import urllib
def my_special_urlencode(params):
return '&'.join('{}={}'.format(urllib.quote(k, ''), urllib.quote(v, '')) for k,v in params.items())
params = {
'response_type': 'token',
'client_id': 'xxxxxx',
'redirect_url': 'http://example.com/callback',
'scope': 'activity heartrate location'
}
print my_special_urlencode(params)
结果:
redirect_url=http%3A%2F%2Fexample.com%2Fcallback&response_type=token&client_id=xxxxxx&scope=activity%20heartrate%20location
这是查询中空格的正确编码。试试
”http://www.google.com/?“+urllib.urlencode({“q”:“http query string”})
@zvone我需要空格作为%20个编码空格,因为+不正确,但它不是不正确的+
是querystring参数中空格的正确编码%20
是路径元素中空格的编码。这是查询中空格的正确编码。试试”http://www.google.com/?“+urllib.urlencode({“q”:“http query string”})
@zvone我需要空格作为%20个编码空格,因为+不正确,但它不是不正确的+
是querystring参数中空格的正确编码<代码>%20是路径元素中空间的编码。这是我得到的重定向\u url=http%3A%2F%2F127.0.0.1%3A8084%2faile\u healtg%2Faccess\u令牌%2F&response\u type=token&client\u id=xxxx&scope=activity%2Bheartrate%2b位置我没有得到%20@George数据必须使用urlencode
方法和其他参数对url进行编码使用quote
。这就是我得到的重定向\u url=http%3A%2F%2F127.0.0.1%3A8084%2faile\u healtg%2faile\u访问\u令牌%2F&response\u type=token&client\u id=xxxx&scope=activity%2Bheartrate%2b位置我没有得到%20@George数据必须使用urlencode
方法对url进行编码,并使用quote
对其他参数进行编码。是的这就是解决方案是的,这就是解决方案