Python 将字典字符串转换为字典列表
我有一个包含多个字典(1到n)的字符串,我想将其更改为字典列表,如果它有多个字典,则可以正常工作:Python 将字典字符串转换为字典列表,python,list,dictionary,Python,List,Dictionary,我有一个包含多个字典(1到n)的字符串,我想将其更改为字典列表,如果它有多个字典,则可以正常工作: str_dic = '{"Name": "banana", "Color": "yellow", "Count": "three"}, {"Name": "apple", "Color": "red"
str_dic = '{"Name": "banana", "Color": "yellow", "Count": "three"}, {"Name": "apple", "Color": "red", "Count": "five"}'
lst = list(eval(str_dic))
print(type(lst))
print(lst)
>>> <class 'list'>
>>> [{'Name': 'banana', 'Color': 'yellow', 'Count': 'three'}, {'Name': 'apple', 'Color': 'red', 'Count': 'five'}]
str_dic='{“Name”:“banana”,“Color”:“yellow”,“Count”:“three”},{“Name”:“apple”,“Color”:“red”,“Count”:“five”}
lst=列表(评估(str_dic))
打印(类型(lst))
打印(lst)
>>>
>>>[{'Name':'banana','Color':'yellow','Count':'three'},{'Name':'apple','Color':'red','Count':'five'}]
但是当字符串只包含一个字典时,它只返回键,不返回值
str_dic = '{"Name": "banana", "Color": "yellow", "Count": "three"}'
lst = list(eval(str_dic))
print(type(lst))
print(lst)
>>> <class 'list'>
>>> ['Name', 'Color', 'Count']
str_dic='{“Name”:“banana”,“Color”:“yellow”,“Count”:“three”}'
lst=列表(评估(str_dic))
打印(类型(lst))
打印(lst)
>>>
>>>[‘名称’、‘颜色’、‘计数’]
在eval之前向字符串添加方括号可以解决此问题:
str_dic = '[{"Name": "banana", "Color": "yellow", "Count": "three"}, {"Name": "apple", "Color": "red", "Count": "five"}]'
lst = eval(str_dic)
print(lst)
str_dic = '[{"Name": "banana", "Color": "yellow", "Count": "three"}]'
lst = eval(str_dic)
print(lst)
输出:
[{'Name': 'banana', 'Color': 'yellow', 'Count': 'three'}]
[{'Name': 'banana', 'Color': 'yellow', 'Count': 'three'}, {'Name': 'apple', 'Color': 'red', 'Count': 'five'}]
在eval之前向字符串添加方括号可以解决此问题:
str_dic = '[{"Name": "banana", "Color": "yellow", "Count": "three"}, {"Name": "apple", "Color": "red", "Count": "five"}]'
lst = eval(str_dic)
print(lst)
str_dic = '[{"Name": "banana", "Color": "yellow", "Count": "three"}]'
lst = eval(str_dic)
print(lst)
输出:
[{'Name': 'banana', 'Color': 'yellow', 'Count': 'three'}]
[{'Name': 'banana', 'Color': 'yellow', 'Count': 'three'}, {'Name': 'apple', 'Color': 'red', 'Count': 'five'}]
由于您使用的是
JSON
数据,因此需要使用Python的内置模块JSON
。快速阅读注释将解释代码
import json
str_dic = '...'
str_dic_list = str_dic.split('}, ') # Split string dictionaries into list (see Note)
dic_list = [] # Initialize an empty list for storing dictionaries
for dic in str_dic_list: # Iterate through the list of string dictionaries
temp_dic = dic.rstrip('}') + '}' # Adds trailing "}" at the end of string dictionary (see Note)
d = json.loads(temp_dic) # Convert string dictionary (JSON) into dictionary
dic_list.append(d) # Append the converted dictionary in the list
注意:
这里,我们使用str_dic.split('}',)
,因为使用str_dic.split('',')
将拆分词典中的每个条目,而不是拆分词典本身。例如,它可能会导致{'Name':'banana'
,而不是{'Name':'banana','Color':'yellow','Count':'three'}
。不完全是这样。我们在每个结果的末尾都会丢失{'Name':'banana','Color':'yellow','Count':'three'
(注意缺少的
)。因此,为了补偿这一点,我们将在末尾连接花括号:dic+'}'
。但这可能会导致最后一个字符串字典出现问题,因为它将保留大括号,因为拆分不会在该点发生。因此,我们要确保去掉大括号的末端,然后将其连接起来,因此,dic.rstrip('}')+'}'
输出
由于您使用的是
JSON
数据,因此需要使用Python的内置模块JSON
。快速阅读注释将解释代码
import json
str_dic = '...'
str_dic_list = str_dic.split('}, ') # Split string dictionaries into list (see Note)
dic_list = [] # Initialize an empty list for storing dictionaries
for dic in str_dic_list: # Iterate through the list of string dictionaries
temp_dic = dic.rstrip('}') + '}' # Adds trailing "}" at the end of string dictionary (see Note)
d = json.loads(temp_dic) # Convert string dictionary (JSON) into dictionary
dic_list.append(d) # Append the converted dictionary in the list
注意:
这里,我们使用str_dic.split('}',)
,因为使用str_dic.split('',')
将拆分词典中的每个条目,而不是拆分词典本身。例如,它可能会导致{'Name':'banana'
,而不是{'Name':'banana','Color':'yellow','Count':'three'}
。不完全是这样。我们在每个结果的末尾都会丢失{'Name':'banana','Color':'yellow','Count':'three'
(注意缺少的
)。因此,为了补偿这一点,我们将在末尾连接花括号:dic+'}'
。但这可能会导致最后一个字符串字典出现问题,因为它将保留大括号,因为拆分不会在该点发生。因此,我们要确保去掉大括号的末端,然后将其连接起来,因此,dic.rstrip('}')+'}'
输出
简单高效。但是,小心不要对非安全的用户输入字符串调用
eval
。使用json.loads()
而不是eval()
。简单有效。但是,请注意不要对非安全用户输入的字符串调用eval
。请使用json.loads()
而不是eval()
。在上一个示例中,尝试将list(eval(str_dic))
替换为eval(str_dic)
<代码>评估(str_dic)成为字典;尝试将字典转换为这样的列表时,只保留键并丢弃值。使用逗号,它将成为元组而不是单个dict。为什么要以这种…奇怪…格式开始dict字符串?您永远不必eval
数据,您应该以安全且明确可解析的格式保存数据,如JSON列表。在上一个示例中,尝试将list(eval(str_dic))
替换为eval(str_dic)
<代码>评估(str_dic)成为字典;尝试将字典转换为这样的列表时,只保留键并丢弃值。使用逗号,它将成为元组而不是单个dict。为什么要以这种…奇怪…格式开始dict字符串?您不应该评估数据,您应该以安全且明确可解析的格式保存数据,类似于JSON列表。它似乎工作不正常并出现错误。@Meysam我已使用所需的解释更新了代码…它似乎工作不正常并出现错误。@Meysam我已使用所需的解释更新了代码。。。