Python,相反函数 urllib.urlencode

发布于 2024-09-15 11:39:50 字数 87 浏览 2 评论 0原文

处理urllib.urlencode后的数据如何转换为dict? urllib.urldecode 不存在。

How can I convert data after processing urllib.urlencode to dict?
urllib.urldecode does not exist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∝单色的世界 2024-09-22 11:39:51

urllib.unquote_plus()做你想做的事。它将 %xx 转义符替换为其单字符等效项,并将加号替换为空格。

示例:

unquote_plus('/%7Ecandidates/?name=john+connolly') 

产量

'/~candidates/?name=john connolly'.

urllib.unquote_plus() does what you want. It replaces %xx escapes by their single-character equivalent and replaces plus signs with spaces.

Example:

unquote_plus('/%7Ecandidates/?name=john+connolly') 

yields

'/~candidates/?name=john connolly'.
梦醒时光 2024-09-22 11:39:50

正如 urlencode 的文档所说,

urlparse 模块提供了
函数 parse_qs() 和 parse_qsl()
用于解析查询字符串
转换为 Python 数据结构。

(在较旧的 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']}

原始字典 d 和“往返”字典 d1 之间的明显区别在于后者具有(在本例中为单项) 列出作为值——这是因为查询字符串中没有唯一性保证,并且对于您的应用来说,了解为每个键指定了哪些多个值可能很重要(也就是说,列表获胜)并不总是单项的;-)。

作为替代方案:

>>> sq = urlparse.parse_qsl(s)
>>> sq  
[('a', 'b'), ('c', 'd')]
>>> dict(sq)
{'a': 'b', 'c': 'd'}

您可以获得一系列对(urlencode 也接受这样的参数 - 在这种情况下它保留顺序,而在 dict 情况下没有顺序可以保留;-)。如果您知道没有重复的“键”,或者不关心是否有重复的“键”,那么(如我所示)您可以调用 dict 来获取包含非列表值的字典。然而,一般来说,您确实需要考虑如果存在重复项,您想要做什么(Python 不会代表您决定;-)。

As the docs for urlencode say,

The urlparse module provides the
functions parse_qs() and parse_qsl()
which are used to parse query strings
into Python data structures.

(In older Python releases, they were in the cgi module). So, for example:

>>> 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 d and the "round-tripped" one d1 is that the latter has (single-item, in this case) lists as 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;-).

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 dict to get a dictionary with non-list values. In general, however, you do need to consider what you want to do if duplicates are present (Python doesn't decide that on your behalf;-).

浅浅淡淡 2024-09-22 11:39:50

Python 3 版本 基于 Alex 的答案:

>>> import urllib.parse
>>> d = {'a':'x', 'b':'', 'c':'z'}
>>> s = urllib.parse.urlencode(d)
>>> s
'a=x&b=&c=z'
>>> d1 = urllib.parse.parse_qs(s, keep_blank_values=True)
>>> d1
{'a': ['x'], 'b': [''], 'c': ['z']}

替代方案:

>>> sq = urllib.parse.parse_qsl(s, keep_blank_values=True)
>>> sq
[('a', 'x'), ('b', ''), ('c', 'z')]
>>> dict(sq)
{'a': 'x', 'b': '', 'c': 'z'}

parse_qsl 是可逆的:

>>> urllib.parse.urlencode(sq)
'a=x&b=&c=z'

在解析用户输入时,请记住可能的重复项:

>>> s = 'a=x&b=&a=z'
>>> d1 = urllib.parse.parse_qs(s, keep_blank_values=True)
>>> d1
{'a': ['x', 'z'], 'b': ['']}
>>> sq = urllib.parse.parse_qsl(s, keep_blank_values=True)
>>> sq
[('a', 'x'), ('b', ''), ('a', 'z')]
>>> dict(sq)
{'a': 'z', 'b': ''}
  1. parse_qs 结果中的列表可能有多个项目
  2. parse_qsl 上调用 dict > 结果可能隐藏值

Python 3 version based on Alex's answer:

>>> import urllib.parse
>>> d = {'a':'x', 'b':'', 'c':'z'}
>>> s = urllib.parse.urlencode(d)
>>> s
'a=x&b=&c=z'
>>> d1 = urllib.parse.parse_qs(s, keep_blank_values=True)
>>> d1
{'a': ['x'], 'b': [''], 'c': ['z']}

The alternative:

>>> sq = urllib.parse.parse_qsl(s, keep_blank_values=True)
>>> sq
[('a', 'x'), ('b', ''), ('c', 'z')]
>>> dict(sq)
{'a': 'x', 'b': '', 'c': 'z'}

parse_qsl is reversible:

>>> urllib.parse.urlencode(sq)
'a=x&b=&c=z'

Keep possible duplicates in mind, when parsing user-input:

>>> s = 'a=x&b=&a=z'
>>> d1 = urllib.parse.parse_qs(s, keep_blank_values=True)
>>> d1
{'a': ['x', 'z'], 'b': ['']}
>>> sq = urllib.parse.parse_qsl(s, keep_blank_values=True)
>>> sq
[('a', 'x'), ('b', ''), ('a', 'z')]
>>> dict(sq)
{'a': 'z', 'b': ''}
  1. The lists in the parse_qs result may have more than one item
  2. Calling dict on the parse_qsl result may hide values
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文