Python,相反的函数 urllib.urlencode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3542881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python, opposite function urllib.urlencode
提问by Artyom
How can I convert data after processing urllib.urlencodeto dict?
urllib.urldecodedoes not exist.
处理后如何将数据转换urllib.urlencode为dict?
urllib.urldecode不存在。
采纳答案by Alex Martelli
As the docsfor urlencodesay,
由于该文档的urlencode发言权,
The urlparse module provides the functions parse_qs() and parse_qsl() which are used to parse query strings into Python data structures.
urlparse 模块提供了函数 parse_qs() 和 parse_qsl() 用于将查询字符串解析为 Python 数据结构。
(In older Python releases, they were in the cgimodule). So, for example:
(在较旧的 Python 版本中,它们位于cgi模块中)。因此,例如:
>>> import urllib
>>> import urlparse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urlparse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The obvious difference between the original dictionary dand the "round-tripped" one d1is that the latter has (single-item, in this case) listsas values -- that's because there is no uniqueness guarantee in query strings, and it may be important to your app to know about what multiple values have been given for each key (that is, the lists won't always be single-item ones;-).
原始字典d和“往返”字典之间的明显区别在于d1后者具有(在这种情况下为单个项目)列表作为值——这是因为查询字符串中没有唯一性保证,这可能很重要到您的应用程序以了解为每个键指定了哪些多个值(即,列表并不总是单项的;-)。
As an alternative:
作为备选:
>>> sq = urlparse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
you can get a sequence of pairs (urlencode accepts such an argument, too -- in this case it preserves order, while in the dict case there's no order to preserve;-). If you know there are no duplicate "keys", or don't care if there are, then (as I've shown) you can call dictto get a dictionary with non-list values. In general, however, you do need to consider what you want to do if duplicates arepresent (Python doesn't decide that on your behalf;-).
您可以获得一系列对(urlencode 也接受这样的参数 - 在这种情况下它保留顺序,而在 dict 情况下没有保留顺序;-)。如果您知道没有重复的“键”,或者不关心是否有重复的“键”,那么(如我所示)您可以调用dict以获取具有非列表值的字典。但一般情况下,你需要考虑你想要做什么,如果重复的存在(Python没有决定代表你;-)。
回答by phobie
Python 3 codefor Alex's solution:
Alex 解决方案的Python 3 代码:
>>> import urllib.parse
>>> d = {'a':'b', 'c':'d'}
>>> s = urllib.parse.urlencode(d)
>>> s
'a=b&c=d'
>>> d1 = urllib.parse.parse_qs(s)
>>> d1
{'a': ['b'], 'c': ['d']}
The alternative:
替代方案:
>>> sq = urllib.parse.parse_qsl(s)
>>> sq
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}
parse_qsl is reversible:
parse_qsl 是可逆的:
>>> urllib.parse.urlencode(sq)
'a=b&c=d'
回答by Andrew Farrell
urllib.unquote_plus()does what you want. It replaces %xx escapes by their single-character equivalent and replaces plus signs with spaces.
urllib.unquote_plus()做你想做的。它用等效的单字符替换 %xx 转义符,并用空格替换加号。
Example:
例子:
unquote_plus('/%7Ecandidates/?name=john+connolly')
yields
产量
'/~candidates/?name=john connolly'.

