comprehension to change nested list to list of dictionaries

0

I've massaged the data into the list structure…

[['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']]

I'm trying to use a comprehension to take each item in the list and split it into a dictionary with the format…

['{'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) '}']

The following for loop seems to work…

newdata = []
for item in data:
    eachdict = {}
    for each in item:
        new = each.split(':', 1)
        eachdict[new[0]] = new[1]
        newdata.append(eachdict)

But my attempt at a comprehension does not…

newdata = [[{key:value for item in data} for line in item] for key, value in (line.split(':', 1))]

This comprehension runs, but it doesn't have the nesting done correctly…

newdata = [{key:value for item in data} for key, value in (item.split(':', 1),)]

I've just started learning comprehensions and I've been able to use them successfully to get the data into the above format of a nested list, but I'm struggling to understand the nesting when I'm going down three levels and switching from list to dictionary.

I'd appreciate some pointers on how to tackle the problem.

For bonus points, I'll need to split the long string inside the attributes key into a dictionary of dictionaries as well. I'd like to be able to reference the 'Alis' key, the 'labl' key and so on. I can probably figure that out on my own if I learn how to use nested comprehensions in the example above first.

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"' 

For context…

I'm using the output of "security dump-keychain" on the Mac to make a nice Python data structure to find keys. The check_output of this command is a long string with some inconsistent formatting and embedded newlines that I need to clean up to get the data into a list of dictionaries of dictionaries.

For those interested in Mac admin topics, this is so we can remove items that save the Active Directory password when the AD password is reset so that the account doesn't get locked by, say, Outlook presenting the old password to Exchange over and over.

python
list
dictionary
list-comprehension
dictionary-comprehension
asked on Stack Overflow Feb 15, 2019 by weldon

1 Answer

1

Here might be an approach:

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'}]

The split breaks up each individual string into a key, value pair. The inside list comprehension loops through each key, value pair in an individual item. The outside list comprehension loops through each item in the main list.

answered on Stack Overflow Feb 15, 2019 by Tammo Heeren

User contributions licensed under CC BY-SA 3.0