我已将数据整理到列表结构中……
[['keychain: "keychainname.keychain-db"',
'version: 512',
'class: 0x0000000F ',
'attributes:\n long string containing : and \n that needs to be split up into a list (by newline) of dictionaries (by regex and/or split() function) '],
['keychain: "keychainname.keychain-db"',
'version: 512',
'class: 0x0000000F ',
'attributes:\n long string that needs to be split up '],
['keychain: "keychainname.keychain-db"',
'version: 512',
'class: 0x0000000F ',
'attributes:\n long string that needs to be split up']]
我正在尝试使用一种理解方法来获取列表中的每个项目,并将其拆分为以下格式的字典:
['{'keychain': 'keychainname.db',
'version': '512',
'class': '0x0000000F',
'attribute':\n long string containing : and \n that needs to be split up into a dictionary (by newline) of dictionaries (by regex and/or split() function) '}']
以下for循环似乎有效……
newdata = []
for item in data:
eachdict = {}
for each in item:
new = each.split(':', 1)
eachdict[new[0]] = new[1]
newdata.append(eachdict)
但是我的理解力却没有……
newdata = [[{key:value for item in data} for line in item] for key, value in (line.split(':', 1))]
此理解已运行,但嵌套未正确完成...
newdata = [{key:value for item in data} for key, value in (item.split(':', 1),)]
我刚刚开始学习理解,并且已经能够成功地使用它们将数据转换为嵌套列表的上述格式,但是当我进入三个层次时,我一直在努力理解嵌套然后从列表切换到字典。
我希望对如何解决该问题提出一些建议。
要获得加分,我还需要将attribute键中的长字符串也拆分成字典字典。我希望能够引用“ Alis”键,“ labl”键等。如果我首先在上面的示例中学习如何使用嵌套的理解,我可能可以自己弄清楚。
attributes:\n
"alis"<blob>="com.company.companyvpn.production.vpn.5D5AF9C525C25350E9CD5CEBED824BFD60E42110"\n
"cenc"<uint32>=0x00000003 \n
"ctyp"<uint32>=0x00000001 \n
"hpky"<blob>=0xB7262C7D5BCC976744F8CA6DE5A80B755622D434 "\\267&,}[\\314\\227gD\\370\\312m\\345\\250\\013uV"\\3244"\n
"issu"<blob>=0x306E3128302606035504030C1F4170706C6520436F72706F726174652056504E20436C69656E7420434120313120301E060355040B0C1743657274696669636174696F6E20417574686F7269747931133011060355040A0C0A4170706C6520496E632E310B3009060355040613025553 "0n1(0&\\006\\003U\\004\\003\\014\\037Company Corporate VPN Client CA 11 0\\036\\006\\003U\\004\\013\\014\\027Certification Authority1\\0230\\021\\006\\003U\\004\\012\\014\\012Company Inc.1\\0130\\011\\006\\003U\\004\\006\\023\\002US"\n
"labl"<blob>="com.company.companyvpn.production.vpn.5D5AF9C525C25350E9CD5CEBED824BFD60E42110"\n
"skid"<blob>=0xB7262C7D5BCC976744F8CA6DE5A80B755622D434 "\\267&,}[\\314\\227gD\\370\\312m\\345\\250\\013uV"\\3244"\n "snbr"<blob>=0x060A02F6F9880D69 "\\006\\012\\002\\366\\371\\210\\015i"\n
"subj"<blob>=0x3061315F305D06035504030C56636F6D2E6170706C652E6973742E64732E6170706C65636F6E6E656374322E70726F64756374696F6E2E76706E2E35443541463943353235433235333530453943443543454245443832344246443630453432313130 "0a1_0]\\006\\003U\\004\\003\\014Vcom.company.companyvpn.production.vpn.5D5AF9C525C25350E9CD5CEBED824BFD60E42110"'
对于上下文...
我正在Mac上使用“ security dump-keychain”的输出来制作一个不错的Python数据结构来查找密钥。该命令的check_output是一个长字符串,带有一些不一致的格式和嵌入的换行符,我需要清理掉换行符才能将数据放入字典的字典列表中。
对于那些对Mac管理员主题感兴趣的人,这是我们可以删除重置AD密码时保存Active Directory密码的项目,这样该帐户就不会被锁定,例如,Outlook向Exchange展示旧密码一遍又一遍。
答案 0 :(得分:1)
这可能是一种方法:
data = [['keychain: "keychainname.keychain-db"', 'version: 512', 'class: 0x0000000F ', 'attributes:\n long string containing : and \n that needs to be split up into a list (by newline) of dictionaries (by regex and/or split() function) '], ['keychain: "keychainname.keychain-db"', 'version: 512', 'class: 0x0000000F ', 'attributes:\n long string that needs to be split up '], ['keychain: "keychainname.keychain-db"', 'version: 512', 'class: 0x0000000F ', 'attributes:\n long string that needs to be split up']]
result = [dict([item.split(':', 1) for item in items]) for items in data]
>>> [{'keychain': ' "keychainname.keychain-db"', 'version': ' 512', 'class': ' 0x0000000F ', 'attributes': '\n long string containing : and \n that needs to be split up into a list (by newline) of dictionaries (by regex and/or split() function) '}, {'keychain': ' "keychainname.keychain-db"', 'version': ' 512', 'class': ' 0x0000000F ', 'attributes': '\n long string that needs to be split up '}, {'keychain': ' "keychainname.keychain-db"', 'version': ' 512', 'class': ' 0x0000000F ', 'attributes': '\n long string that needs to be split up'}]
split
将每个字符串分成一个键,值对。
内部列表理解在单个项目中的每个键,值对之间循环。外部列表理解遍历主列表中的每个项目。