我需要在 Python 中生成对 PHP 解析器有效的 urlencoded 字符串
我需要在 Python 中生成与在 PHP 中相同的输出,可能吗?
Php 代码:
<?php
$items = array(
array(
'item_key_a' => 'item_value_a')
);
$payload = array(
'i_key' => 'i_value',
'items' => $items,
);
echo http_build_query($payload);
?>
输出 urlencoded:
i_key=i_value&items%5B0%5D%5Bitem_key_a%5D=item_value_a
输出 urldecoded:
i_key=i_value&items[0][item_key_a]=item_value_a
Python 代码:
item = {}
item['item_key_a'] = 'item_value_a'
data = {}
data['i_key'] = 'i_value'
data['items'] = [item]
import urllib
print urllib.urlencode(data)
输出 urlencoded:
items=%5B%7B%27item_key_a%27%3A+%27item_value_a%27%7D%5D&i_key=i_value
输出 urldecoded:
items=[{'item_key_a':+'item_value_a'}]&i_key=i_value
所以在 Python 中,我没有获得 PHP 应用程序所需的有效 urlencoding。
I need to generate same output in Python as I have in PHP, possible?
Php code:
<?php
$items = array(
array(
'item_key_a' => 'item_value_a')
);
$payload = array(
'i_key' => 'i_value',
'items' => $items,
);
echo http_build_query($payload);
?>
Output urlencoded:
i_key=i_value&items%5B0%5D%5Bitem_key_a%5D=item_value_a
Output urldecoded:
i_key=i_value&items[0][item_key_a]=item_value_a
Python code:
item = {}
item['item_key_a'] = 'item_value_a'
data = {}
data['i_key'] = 'i_value'
data['items'] = [item]
import urllib
print urllib.urlencode(data)
Output urlencoded:
items=%5B%7B%27item_key_a%27%3A+%27item_value_a%27%7D%5D&i_key=i_value
Output urldecoded:
items=[{'item_key_a':+'item_value_a'}]&i_key=i_value
So in Python I don't get valid urlencoding that I need for PHP app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Python urlencode 不处理嵌套字典。您需要以如下方式自己编写:
Python urlencode does not handle nested dict. You need to write it yourself in a manner such as presented for this question:
现在似乎有一个在Python中处理这个问题的库
https://github.com/uber/multiDimension_urlencode
There now seems to be a library for handling this in Python
https://github.com/uber/multidimensional_urlencode