简体   繁体   English

在python中如何将字典值从字符串更改为字典?

[英]In python how do I change dictionary values from strings into dictionaries?

I have dictionary in the form : d={741178: u'{"width":37.8365,"height":150,"length":234},{"width":35.7,"height":178,"length":122}',741179: u'{"width":98.67,"height":180,"length":994},{"width":98.79,"height":167.8,"length":154.22}'}我有以下形式的字典: d={741178: u'{"width":37.8365,"height":150,"length":234},{"width":35.7,"height":178,"length":122}',741179: u'{"width":98.67,"height":180,"length":994},{"width":98.79,"height":167.8,"length":154.22}'}

How can I turn it into a dictionary of dictionaries?我怎样才能把它变成字典的字典? I have tried:我试过了:

for k, v in d.items():
    v = ast.literal_eval(v)

But this does not work.但这不起作用。 Since each of my pseudo nested dictionaries is actually a string, how do I change their data type within the dictionary?由于我的每个伪嵌套字典实际上都是一个字符串,我如何在字典中更改它们的数据类型?

You can either use ast.literal_eval() or json.loads() , the output is almost the same in this case.您可以使用ast.literal_eval()json.loads() ,在这种情况下输出几乎相同。 For JSON, you must enclose the strings in square brackets, though, because they both contain two dictionaries separated by a comma, so the only reasonable way to grab them is as a list of dictionaries.但是,对于 JSON,您必须将字符串括在方括号中,因为它们都包含两个以逗号分隔的字典,因此获取它们的唯一合理方法是将它们作为字典列表。 For Python literals, leaving the square brackets out (or replacing them with parentheses) causes each pair of dictionaries separated by comma to be interpreted as a 2-tuple (should be a double, but that already has a different meaning).对于 Python 文字,将方括号去掉(或用括号替换它们)会导致每对以逗号分隔的字典被解释为 2 元组(应该是双精度数,但已经具有不同的含义)。

I personally recommend using json.loads() , because JSON is currently the most used format for data interchange in new projects.我个人推荐使用json.loads() ,因为 JSON 目前是新项目中最常用的数据交换格式。 It would be best if the source of your strings could add the square brackets in order to produce valid JSON.如果您的字符串源可以添加方括号以生成有效的 JSON,那将是最好的。

import ast
import json

d = {741178: u'{"width":37.8365,"height":150,"length":234},{"width":35.7,"height":178,"length":122}',
     741179: u'{"width":98.67,"height":180,"length":994},{"width":98.79,"height":167.8,"length":154.22}'}

p = {k: ast.literal_eval(v)   for k, v in d.items()}
j = {k: json.loads('['+v+']') for k, v in d.items()}

print(p)
print(j)

# {741178: ({'width': 37.8365, 'height': 150, 'length': 234}, {'width': 35.7, 'height': 178, 'length': 122}), 741179: ({'width': 98.67, 'height': 180, 'length': 994}, {'width': 98.79, 'height': 167.8, 'length': 154.22})}
# {741178: [{'width': 37.8365, 'height': 150, 'length': 234}, {'width': 35.7, 'height': 178, 'length': 122}], 741179: [{'width': 98.67, 'height': 180, 'length': 994}, {'width': 98.79, 'height': 167.8, 'length': 154.22}]}

As you can see, the only difference is ( )[ ]如您所见,唯一的区别是( )[ ]

I have corrected the json and removed un-necessary double quotes and commas If you want to have dictionary as values you have to have the values as an array of dictionaries.我已经更正了 json 并删除了不必要的双引号和逗号如果您想将字典作为值,您必须将值作为字典数组。

import json    
d={741178:
       u'{"width":37.8365,"height":150,"length":234},'
       u'{"width":35.7,"height":178,"length":122}',
   741179: u'{"width":98.67,"height":180,"length":994},'
           u'{"width":98.79,"height":167.8,"length":154.22}'

   }
for key, value in d.items():
    d[key]= json.loads('['+value+']')
print(d)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字典值从 python 中的字典列表更改为字典 - change dictionary values to dictionaries from list of dictionaries in python 如何从词典列表中调用词典值? - how do i call dictionary values from within a list of dictionaries ? Python如何做列表字典字典的.values()。values() - Python how to do .values().values() of a dictionary of dictionaries of lists 如何从 Python 中的嵌套字典中提取字符串 - How do I extract strings from a nested dictionary in Python 如何动态创建和引用作为字典中值的字典? - How do I create and reference dictionaries that are the values within a dictionary dynamically? 我如何遍历一个字典并从另一本字典返回值? - how do i iterate through one dictionaries and return values from another dictionary? 如何在循环中从字典中用值替换 pandas 数据帧中的字符串中的键? - How do I replace keys in strings in a pandas data frame in a loop from dictionaries with values? 如何在Python中按字典的姓氏对字典列表进行排序? - How do I sort a list of dictionaries by last name of the dictionary in Python? 如何将字典列表转换为Python中的列表字典? - How do I convert a list of dictionaries to a dictionary of lists in Python? Python-如果字符串列表不在字典键中,则从字典列表中删除字典 - Python - Remove dictionary from list of dictionaries if list of strings not in dictionary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM