如何在Python中将字典转换为查询字符串?

发布于 2024-12-10 04:40:35 字数 85 浏览 7 评论 0原文

使用cgi.parse_qs()后,如何将结果(字典)转换回查询字符串?寻找类似于 urllib.urlencode() 的东西。

After using cgi.parse_qs(), how to convert the result (dictionary) back to query string? Looking for something similar to urllib.urlencode().

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

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

发布评论

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

评论(4

夜血缘 2024-12-17 04:40:35

蟒蛇3

urllib.parse.urlencode(查询,doseq=False,[...])

映射对象或可能包含 str 或 bytes 对象的二元素元组序列转换为百分比编码的 ASCII 文本字符串。

Python 3 urllib.parse< /code> 文档


dict 是一个映射。

遗留Python

urllib.urlencode(查询[, doseq])
映射对象或二元素元组序列转换为“百分比编码”字符串...一系列由'分隔的key=value对&' 字符...

Python 2.7 urllib 文档


Python 3

urllib.parse.urlencode(query, doseq=False, [...])

Convert a mapping object or a sequence of two-element tuples, which may contain str or bytes objects, to a percent-encoded ASCII text string.

Python 3 urllib.parse docs

A dict is a mapping.

Legacy Python

urllib.urlencode(query[, doseq])
Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string... a series of key=value pairs separated by '&' characters...

Python 2.7 urllib docs

情魔剑神 2024-12-17 04:40:35

在 python3 中,略有不同:

from urllib.parse import urlencode
urlencode({'pram1': 'foo', 'param2': 'bar'})

输出:'pram1=foo¶m2=bar'

对于 python2 和 python3 兼容性,请尝试以下操作:

try:
    #python2
    from urllib import urlencode
except ImportError:
    #python3
    from urllib.parse import urlencode

In python3, slightly different:

from urllib.parse import urlencode
urlencode({'pram1': 'foo', 'param2': 'bar'})

output: 'pram1=foo¶m2=bar'

for python2 and python3 compatibility, try this:

try:
    #python2
    from urllib import urlencode
except ImportError:
    #python3
    from urllib.parse import urlencode
℉絮湮 2024-12-17 04:40:35

您正在寻找完全类似urllib.urlencode()的东西!

但是,当您调用 parse_qs() (与 parse_qsl()),字典键是唯一的查询变量名称,值是每个名称的值列表

为了将此信息传递到 urllib.urlencode() ,您必须“展平”这些列表。以下是如何使用元组的列表理解来做到这一点:

query_pairs = [(k,v) for k,vlist in d.iteritems() for v in vlist]
urllib.urlencode(query_pairs)

You're looking for something exactly like urllib.urlencode()!

However, when you call parse_qs() (distinct from parse_qsl()), the dictionary keys are the unique query variable names and the values are lists of values for each name.

In order to pass this information into urllib.urlencode(), you must "flatten" these lists. Here is how you can do it with a list comprehenshion of tuples:

query_pairs = [(k,v) for k,vlist in d.iteritems() for v in vlist]
urllib.urlencode(query_pairs)
唔猫 2024-12-17 04:40:35

也许您正在寻找这样的东西:

def dictToQuery(d):
  query = ''
  for key in d.keys():
    query += str(key) + '=' + str(d[key]) + "&"
  return query

它需要一个字典并将其转换为查询字符串,就像 urlencode 一样。它将附加最后一个“&”到查询字符串,但 return query[:-1] 解决了这个问题(如果有问题)。

Maybe you're looking for something like this:

def dictToQuery(d):
  query = ''
  for key in d.keys():
    query += str(key) + '=' + str(d[key]) + "&"
  return query

It takes a dictionary and convert it to a query string, just like urlencode. It'll append a final "&" to the query string, but return query[:-1] fixes that, if it's an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文