Python 3.x 使用python将字典格式的字符串转换为字典
我有一个字典格式的字符串,如:Python 3.x 使用python将字典格式的字符串转换为字典,python-3.x,dictionary,Python 3.x,Dictionary,我有一个字典格式的字符串,如: {'fossils': [{Synset('dodo.n.01'): {'of', 'is', 'out', 'someone', 'fashion', 'whose', 'style'},Synset('fossil.n.02'): {'age', 'that', 'an', 'has', 'past', 'from', 'excavated', 'plant', '(',')', 'and', 'animal', 'in', 'remains', 'ge
{'fossils': [{Synset('dodo.n.01'): {'of', 'is', 'out', 'someone', 'fashion',
'whose', 'style'},Synset('fossil.n.02'): {'age', 'that', 'an', 'has', 'past',
'from', 'excavated', 'plant', '(',')', 'and', 'animal', 'in', 'remains',
'geological', 'soil', 'existed', 'impression', 'of', 'or', 'been', 'the', 'a'},
Synset('fossil.a.01'): {'of', 'a', 'fossil', 'characteristic'}}],
'disturbing': [{Synset('disturb.v.01'): {'me', 'This', 'upset', 'thought','book',
'deeply','move', 'troubling', 'A'}, Synset('agitate.v.06'): {'of', 'or',
'arrangement', 'the', 'position', 'change'}, Synset('touch.v.11'): {'my', '!',
'touch', 'Do', 'tamper', 'with', 'CDs', "n't"}, Synset('interrupt.v.02'): {'of',
'or', 'peace', 'tranquility', 'destroy', 'the', 'I', 'me', 'interrupt', 'Do',
'reading', 'when', "'m", "n't"}}]}
我想把它译成字典。字典的格式是
{key: list of dictionaries as value}
请帮我整理一下
谢谢您有了“对象”(称为Synset)。通常,您可以使用json.loads(str)进行这种转换,以获取字典的值。
因为您拥有这些对象,所以需要手动修复(在json.loads之前预处理字符串),然后再进行后期处理以恢复这些对象
编辑:此外,您有多种类型的偏执狂,在加载字符串时可能会使事情变得复杂
edit2:当然还有另一个选项(如果您的Synset类是已知的,并且您信任它的源):
导入Synset#进行快速转换
dict_val=eval(dict_like_str)
当然,正如我前面提到的那样,我还将在我的原始@student中添加这些行:“此外,您有多种类型的括号,在加载字符串时可能会妨碍操作”。当然还有另一个选项(如果您的Synset类是已知的,并且您信任它的源):'import Synset#for swift conversion dict_val=eval(dict_like_str)`我还将在原始文档中添加这些行