在Python中,可以使用urllib.parse库中的urlencode和parse_qs函数来实现urlencode和urldecode操作。
urlencode函数用于将字典形式的参数编码成URL格式的字符串。例如:from urllib.parse import urlencodeparams = { 'name': 'Alice', 'age': 25, 'city': 'New York'}query_string = urlencode(params)print(query_string)输出结果为:
name=Alice&age=25&city=New+Yorkparse_qs函数用于将URL格式的字符串解码成字典形式的参数。例如:from urllib.parse import parse_qsquery_string = 'name=Alice&age=25&city=New+York'params = parse_qs(query_string)print(params)输出结果为:
{'name': ['Alice'], 'age': ['25'], 'city': ['New York']}这样就可以实现urlencode和urldecode的操作。




