Python 将URL参数转换为字典项

Python 将URL参数转换为字典项

Python的实现了一个称为关联数组的数据结构,通常被称为 字典 。字典由一组键值对组成,每个键值组合都对应一个键和其对应的值。

在本文中,我们将学习一个Python程序将URL参数转换为字典项的方法。

使用的方法

以下是实现此任务的各种方法:

  • 使用urllib.parse.parse_qs()函数

  • 使用setdefault()和re.findall()函数

  • 使用split()函数

示例

假设我们有一个带有URL参数的输入字符串,我们将该查询字符串转换为字典项如下所示。

输入

'tutorials=9&Point=3&Website=2&is=1&best=Yes'

输出

The parsed URL Params of an input string:  {'tutorials': ['9'], 'Point': ['3'], 'Website': ['2'], 'is': ['1'], 'best': ['Yes']}

使用urllib.parse.parse_qs()函数

在这种方法中,我们将使用以下默认的内置函数来完成这个任务;

urllib.parse.parse_qs() function:

解析字符串,键是”=”左边的部分,它返回一个包含参数右边的值的列表。为了使其工作,需要导入外部 urllib.parse()。

步骤

以下是执行所需任务的算法/步骤:

  • 使用 import 关键字导入 urllib.parse 模块。

  • 创建一个变量来存储输入字符串。

  • 打印输入字符串。

  • 使用 urllib.parse.parse_qs() 函数,将输入字符串作为参数传递给它。

  • 上述步骤将给定的 URL 参数(查询字符串)转换为项的字典。

  • 打印输入字符串的解析后的 URL 参数。

示例

以下程序使用 urllib.parse.parse_qs() 函数返回给定的 URL 参数字符串的字典项:

# importing urllib.parse module
import urllib.parse
# input string
inputString = 'tutorials=9&Point=3&Website=2&is=1&best=Yes'
# printing input string
print("Input String:", inputString)
# Pass the input string to parse_qs(query string) to convert to dictionary
resultParams = urllib.parse.parse_qs(inputString)
# printing resultant parsed URLs params of an input string 
print("The parsed URL Params of an input string: ", resultParams)

输出

Input String: tutorials=9&Point=3&Website=2&is=1&best=Yes
The parsed URL Params of an input string:  {'tutorials': ['9'], 'Point': ['3'], 'Website': ['2'], 'is': ['1'], 'best': ['Yes']}

使用setdefault()和re.findall()函数

re.findall()函数 − findall()函数返回字符串中的所有非重叠模式匹配项,以字符串列表的形式返回。字符串从左到右扫描,并按照找到的顺序返回匹配项。

setdefault()方法

setdefault()方法返回具有给定键的项的值。

如果键不存在,则使用给定的值插入它。

语法

dictionary.setdefault(keyname, value)

步骤

以下是执行所需任务的算法/步骤:

  • 使用import关键字导入re(正则表达式)模块。

  • 使用re模块的findall()函数使用正则表达式模式作为参数从输入字符串中获取所有参数。

  • 创建一个新的空字典,用于存储输入字符串的结果解析URL。

  • 使用for循环遍历上述参数字典的键值对。

  • 使用[]运算符和setdefault函数将字典值设置为列表。

  • 使用append()函数将该值附加到字典。

  • 打印输入字符串的结果解析URL参数。

示例

以下程序使用setdefault()和re.findall()函数返回给定URL参数字符串的字典项:

# importing re module
import re
# input string
inputString = 'tutorials=9&Point=3&Website=2&is=1&best=Yes'
# printing input string
print("Input String:", inputString)
# getting all params from input string using regex pattern
all_params = re.findall(r'([^=&]+)=([^=&]+)', inputString)
# Creating a new dictionary to store the URL Parameters as dictionary items
resultantDict = dict()
# traversing through the key, values of above all_params dictionary
for k, v in all_params:
  # Set the value of the dictionary as a list using the [] operator and setdefault function
  # Then append this value to the dictionary 
    resultantDict.setdefault(k, []).append(v)
# printing resultant parsed urls params of input string
print("The parsed urls params of input string:", resultantDict)

输出

在执行时,以上程序将生成以下输出-

Input String: tutorials=9&Point=3&Website=2&is=1&best=Yes
The parsed URLs params of an input string: {'tutorials': ['9'], 'Point': ['3'], 'Website': ['2'], 'is': ['1'], 'best': ['Yes']}

使用split()函数

在这个方法中,我们将使用split()方法将URL参数转换为字典项。

语法

split():

将字符串拆分为列表。我们可以定义分隔符;默认分隔符是任意空白字符。

示例

以下程序使用split()函数返回给定URL参数字符串的字典项 –

# input string
inputString = 'tutorials=9&Point=3&Website=2&is=1&best=Yes'
# printing input string
print("Input String:", inputString)
# creating an empty dictionary for storing all params
resultantDict = dict()
# splitting input string based on & separator
splittedList = inputString.split("&")
# Traverse in the split list using the for loop
for k in splittedList:
  # Split the list again with "=" separator and store the key and value separately
    p,q=k.split("=")
    # Assign the above key with the list value to the result Dictionary
    resultantDict[p]=[q]
# printing resultant parsed URLs params of the input string 
print("The parsed URLs params of input string:", resultantDict)

输出

执行后,上述程序将生成以下输出:

Input String: tutorials=9&Point=3&Website=2&is=1&best=Yes
The parsed URLs params of an input string: {'tutorials': ['9'], 'Point': ['3'], 'Website': ['2'], 'is': ['1'], 'best': ['Yes']}

结论

在这篇文章中,我们学习了使用3种不同的方法将URL参数转换成字典项。使用regex.findall()函数,我们学会了如何使用正则表达式拆分给定的字符串。我们学会了如何使用setdefault()函数将字典的值更改为任何默认值。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程