gpt4 book ai didi

python - 如何将类似字典的字符串转换为字典?

转载 作者:行者123 更新时间:2023-12-04 11:21:40 26 4
gpt4 key购买 nike

我有一个像字符串这样的字典,如下所示:

str = "Access AR1:\n\tTargets: \n\t\tManagement Name:csw_1\n\t\tObject Name:csw_obj_1\n\t\tdetails:103\n\t\tManagement Name:csw_123\n\t\tObject Name:csw_obj_134\n\t\tdetails:123\n\tSources: \n\t\tIP:10.20.30.40\n\t\tSubnet Mask:255.255.255.255\nAccess AR2:\n\tTargets: \n\t\tManagement Name:csw_2\n\t\tObject Name:csw_obj_2\n\t\tdetails:110\n\t\tManagement Name:csw_431\n\t\tObject Name:csw_obj_21\n\t\tdetails:134\n\tSources: \n\t\tIP:10.20.10.10\n\t\tSubnet Mask:255.255.255.192"
这以相同的格式进行,如下所示:
Access AR1:
Targets:
Management Name:csw_1
Object Name:csw_obj_1
details:103
Management Name:csw_123
Object Name:csw_obj_134
details:123
Sources:
IP:10.20.30.40
Subnet Mask:255.255.255.255
Access AR2:
Targets:
Management Name:csw_2
Object Name:csw_obj_2
details:110
Management Name:csw_431
Object Name:csw_obj_21
details:134
Sources:
IP:10.20.10.10
Subnet Mask:255.255.255.192
这需要更改为:
str = {"Access AR1": { "Targets": [{"Management Name:csw_1", "Object Name":"csw_obj_1", "details":"103"}, {"Management Name:csw_123", "Object Name":"csw_obj_134", "details":"123"}]
"Sources": {"IP":"10.20.30.40", "Subnet Mask": "255.255.255.255"}
},
"Access AR2": { "Targets": [{"Management Name:csw_2", "Object Name":"csw_obj_2", "details":"110"}, {"Management Name:csw_431", "Object Name":"csw_obj_21", "details":"133"}]
"Sources": {"IP":"10.20.10.10", "Subnet Mask": "255.255.255.192"}
}
}
我试过 ast.literal_eval, eval并以错误告终。

最佳答案

您的字符串几乎是 yaml - 用一个或多个空格替换\t 并将其加载为 YAML。
第一的:

pip install pyyaml
然后这段代码有效:
import yaml
import pprint

str = "Access AR1:\n\tTargets: \n\t\tManagement Name:csw_1\n\t\tObject Name:csw_obj_1\n\t\tdetails:103\n\tSources: \n\t\tIP:10.20.30.40\n\t\tSubnet Mask:255.255.255.255\nAccess AR2:\n\tTargets: \n\t\tManagement Name:csw_2\n\t\tObject Name:csw_obj_2\n\t\tdetails:110\n\tSources: \n\t\tIP:10.20.10.10\n\t\tSubnet Mask:255.255.255.192"

str1 = str.replace( '\t', ' ' )

res = yaml.load(str1)

pprint.pprint( res )
输出:
{'Access AR1': {'Sources': 'IP:10.20.30.40 Subnet Mask:255.255.255.255',
'Targets': 'Management Name:csw_1 Object Name:csw_obj_1 '
'details:103'},
'Access AR2': {'Sources': 'IP:10.20.10.10 Subnet Mask:255.255.255.192',
'Targets': 'Management Name:csw_2 Object Name:csw_obj_2 '
'details:110'}}
或者,如果你真的想把它作为一个字符串,那么:
str = repr(res)
更新
刚刚意识到,例如'Managent Name:csw_1' 未被检测为键:值。需要正则表达式 re.sub()将这些拆分为单独的行以解决此问题:
import yaml
import pprint
import re

str = "Access AR1:\n\tTargets: \n\t\tManagement Name:csw_1\n\t\tObject Name:csw_obj_1\n\t\tdetails:103\n\tSources: \n\t\tIP:10.20.30.40\n\t\tSubnet Mask:255.255.255.255\nAccess AR2:\n\tTargets: \n\t\tManagement Name:csw_2\n\t\tObject Name:csw_obj_2\n\t\tdetails:110\n\tSources: \n\t\tIP:10.20.10.10\n\t\tSubnet Mask:255.255.255.192"

# replace \t with four-space indent
str1 = str.replace( '\t', ' ' )

# further tweak to split sub-keys like " Management Name:csw_1" onto separate lines
str1 = re.sub(r"^(\s+)(.*?\S:)(\S.*)", r"\1\2\n\1 \3",str1,flags=re.MULTILINE )

res = yaml.load(str1)

pprint.pprint( res )
这是调整后的字符串:
Access AR1:
Targets:
Management Name:
csw_1
Object Name:
csw_obj_1
details:
103
Sources:
IP:
10.20.30.40
Subnet Mask:
255.255.255.255
Access AR2:
Targets:
Management Name:
csw_2
Object Name:
csw_obj_2
details:
110
Sources:
IP:
10.20.10.10
Subnet Mask:
255.255.255.192
结果:
{'Access AR1': {'Sources': {'IP': '10.20.30.40',
'Subnet Mask': '255.255.255.255'},
'Targets': {'Management Name': 'csw_1',
'Object Name': 'csw_obj_1',
'details': 103}},
'Access AR2': {'Sources': {'IP': '10.20.10.10',
'Subnet Mask': '255.255.255.192'},
'Targets': {'Management Name': 'csw_2',
'Object Name': 'csw_obj_2',
'details': 110}}}

关于python - 如何将类似字典的字符串转换为字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68257480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com