Home > Net >  Is this {'': [ {}, {}, {} ]} Dictionary of List of a Dictionary? How can I Split the insid
Is this {'': [ {}, {}, {} ]} Dictionary of List of a Dictionary? How can I Split the insid

Time:01-31

I understand I really need Dictionary and List Comprehension understanding. Spent so much time reading all the articles here in stack overflow and else where but getting no where and more confused. Please someone help.

When I query database, basically I get a <class 'dict'> object back of {'': [ {x1:1, y1:2, z1:3}, {...}, {...} ]} syntax. e.g:

{'results': [{'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\\Norma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}, {'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\\Norma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\Christopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\Christopher.Cheef acknowledged alert Server Node Down on NORTHLOBBY-VM5.'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\Christopher.Cheef has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}]}

I'm trying to get those x's, y's and z's value out separately so I can print them nicely or process them as needed. So something like this

1. 'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\\Norma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'

2. 'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\\Norma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..'

3. 'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\Christopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].'

There are folks with solution using Pandas/Numpy, Zip() functions, dreaded :-) Lambda() function etc... If you can help with simpler solution first that will be great. I'll understand issue better that way.

Here is sample code that I am testing with.

def query_database():
    result = {'results': [{'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\\Norma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}, {'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\\Norma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\Christopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\christopher.Cheef acknowledged alert Server Node Down on NORTHLOBBY-VM5.'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\Christopher.Cheef has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}]}
    return result


def display_data():
    result = query_database()
    print("{0}".format(result))

    for item in result:
        print(item)
        print(f">>> {result.values()}")


def main():
    display_data()

if __name__ == '__main__':
    main()

CodePudding user response:

Using your example data you can just iterate through results key and print whatever you like:

results = {'results': [{'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\\Norma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}, {'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\\Norma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\Christopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\Christopher.Cheef acknowledged alert Server Node Down on NORTHLOBBY-VM5.'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\\Christopher.Cheef has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}]}
for row in results['results']:
    print(', '.join([f'{header}: {value}' for header, value in row.items()]))  
Event Time: 2023-01-25T00:18:32.3900000, AccountID: MICROSOFT\Norma.Cayshun, AuditEventMessage: User MICROSOFT\Norma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].
Event Time: 2023-01-25T00:18:32.3900000, AccountID: MICROSOFT\Norma.Cayshun, AuditEventMessage: User MICROSOFT\Norma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..
Event Time: 2023-01-16T01:55:05.1930000, AccountID: MICROSOFT\Christopher.Cheef, AuditEventMessage: User MICROSOFT\Christopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].
Event Time: 2023-01-16T01:55:05.1930000, AccountID: MICROSOFT\Christopher.Cheef, AuditEventMessage: User MICROSOFT\Christopher.Cheef acknowledged alert Server Node Down on NORTHLOBBY-VM5.
Event Time: 2023-01-16T01:55:05.1930000, AccountID: MICROSOFT\Christopher.Cheef, AuditEventMessage: User MICROSOFT\Christopher.Cheef has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].

That being said, if you are going to do anything downstream in python with this data, I would toss the results values into a dataframe

import pandas as pd 
df = pd.DataFrame(results['results'])
display(df)
 ----- ----------------------------- ----------------------------- --------------------------------------------------- 
| idx |         Event Time          |          AccountID          |                 AuditEventMessage                 |
 ----- ----------------------------- ----------------------------- --------------------------------------------------- 
|   0 | 2023-01-25T00:18:32.3900000 | MICROSOFT\Norma.Cayshun     | User MICROSOFT\Norma.Cayshun has changed alert... |
|   1 | 2023-01-25T00:18:32.3900000 | MICROSOFT\Norma.Cayshun     | User MICROSOFT\Norma.Cayshun acknowledged aler... |
|   2 | 2023-01-16T01:55:05.1930000 | MICROSOFT\Christopher.Cheef | User MICROSOFT\Christopher.Cheef has changed a... |
|   3 | 2023-01-16T01:55:05.1930000 | MICROSOFT\Christopher.Cheef | User MICROSOFT\Christopher.Cheef acknowledged ... |
|   4 | 2023-01-16T01:55:05.1930000 | MICROSOFT\Christopher.Cheef | User MICROSOFT\Christopher.Cheef has changed a... |
 ----- ----------------------------- ----------------------------- --------------------------------------------------- 

Actual display(df) output in vscode, with jupyter addon, and solarized scheme:

enter image description here

  • Related