首页 > 编程知识 正文

在 Python 中将字符串转换为字典

时间:2023-11-26 13:13:10 阅读:309351 作者:VZYQ

我们基于字符串和字典解决了不同的问题。在本教程中,我们将看到如何在 Python 中将字符串转换为字典。

在此之前,让我们快速回忆一下字符串和字典。

字符串被定义为一个字符序列,并使用单引号或双引号来表示。

例如-


flower = 'Rose'
sub = 'Python'
name = 'James'

我们可以使用类型()检查上述变量的数据类型。

字典在 Python 中被定义为一种数据结构,它使用花括号中的键值对。

我们可以在各自键的帮助下访问字典中的值。

字典的例子是-


Subj = {'subj1': 'Computer Science', 'subj2': 'Physics', 'subj3': 'Chemistry', 'subj4': 'Mathematics'}

现在让我们列出可以将字符串转换为字典的方法。

  1. 使用负载()
  2. 使用literal_eval
  3. 使用生成器表达式

是时候详细讨论它们了-

使用 json.loads()

下面的程序展示了如何使用 json.loads()将字符串转换为字典


#using json()
import json
#initialising the string
string_1 = '{"subj1":"Computer Science","subj2":"Physics","subj3":"Chemistry","subj4":"Mathematics"}'
print("String_1 is ",string_1)
#using json.loads()
res_dict=json.loads(string_1)
#printing converted dictionary
print("The resultant dictionary is ",res_dict)

输出:

String_1 is  {"subj1":"Computer Science","subj2":"Physics","subj3":"Chemistry","subj4":"Mathematics"}
The resultant dictionary is  {'subj1': 'Computer Science', 'subj2': 'Physics', 'subj3': 'Chemistry', 'subj4': 'Mathematics'}

说明:

让我们了解一下我们在上面的程序中做了什么-

  1. 在第一步中,我们已经导入了 json 模块。
  2. 之后,我们已经初始化了我们想要转换的字符串。
  3. 现在我们已经简单地在 loads()中传递了‘string _ 1’作为参数。
  4. 最后,在最后一步中,我们显示了结果字典。

使用 ast.literal_eval()

现在我们来看看 ast.literal_eval 如何帮助我们实现目标。

下面的程序说明了同样的情况-


#convert string to dictionary
#using ast()
import ast
#initialising the string
string_1 = '{"subj1":"Computer Science","subj2":"Physics","subj3":"Chemistry","subj4":"Mathematics"}'
print("String_1 is ",string_1)
#using ast.literal_eval
res_dict=ast.literal_eval(string_1)
#printing converted dictionary
print("The resultant dictionary is ",res_dict)

输出:

String_1 is  {"subj1":"Computer Science","subj2":"Physics","subj3":"Chemistry","subj4":"Mathematics"}
The resultant dictionary is  {'subj1': 'Computer Science', 'subj2': 'Physics', 'subj3': 'Chemistry', 'subj4': 'Mathematics'}

说明:

让我们了解一下我们在上面的程序中做了什么-

  1. 第一步,我们已经导入了 ast 模块。
  2. 之后,我们已经初始化了我们想要转换的字符串。
  3. 现在我们已经简单地在 literaleval()中传递了‘string 1’作为参数。
  4. 最后,在最后一步中,我们显示了结果字典。

使用生成器表达式

最后,在最后一个例子中,我们将讨论如何使用生成器表达式。

让我们仔细研究给定的程序。


#convert string to dictionary
#using generator expressions
#initialising the string
string_1 = "subj1 - 10 , subj2 - 20, subj3 - 25, subj4 - 14"
print("String_1 is ",string_1)
#using strip() and split()
res_dict = dict((a.strip(), int(b.strip()))
                     for a, b in (element.split('-')
                                  for element in string_1.split(', ')))
#printing converted dictionary
print("The resultant dictionary is: ", res_dict)
print(type(res_dict))

输出:

String_1 is  subj1 - 10 , subj2 - 20, subj3 - 25, subj4 - 14
The resultant dictionary is:  {'subj1': 10, 'subj2': 20, 'subj3': 25, 'subj4': 14}
<class 'dict'>

是时候检查一下这种方法的解释了-

  1. 在第一步中,我们已经声明了一个字符串,它的值与一个连字符成对,每对用逗号分隔。这些信息很重要,因为它将成为获得所需输出的一个很好的工具。
  2. 此外,我们在 for循环中使用了 strip() 和 split() ,这样我们就可以得到通常格式的字典。
  3. 最后,我们打印了我们创建的字典,并使用 type()验证了它的类型。

结论

在本教程中,我们探索了字符串到字典的转换方法。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。