如何在 Python 中将字典转换为查询字符串? [英] How to convert a dictionary to query string in Python?

查看:64
本文介绍了如何在 Python 中将字典转换为查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

Python 3

<块引用>

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

<块引用>

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

<块引用>

Python 3 urllib.parse 文档

dict 是一个映射.

传统 Python

<块引用>

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

<块引用>

Python 2.7 urllib 文档

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

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

这篇关于如何在 Python 中将字典转换为查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆