我在变量中存储了列表x = ['eventVersion', '1.08'] ['userIdentity', {'type': 'IAMUser', 'principalId': '', 'accountId': '', 'accessKeyId': '', 'userName': 'test'}] ['eventTime', '2021-09-22T13:49:49Z'] ['eventSource', 'signin.amazonaws.com']
我试图将其转换为单个列表,我试图通过列表循环,但只提供'eventVersion', '1.08'。如何循环遍历每个子列表并获得所有信息?
感谢您的帮助
假设这是输入和注释中提供的预期输出,则需要字典将其转换为键:值对格式,不能使用列表。
x = [
['eventVersion', '1.08'],
[{'userIdentity':
{
'type': 'IAMUser',
'principalId': '',
'accountId': '',
'accessKeyId': '',
'userName': 'test'
}
}],
['eventTime', '2021-09-22T13:49:49Z'],
['eventSource', 'signin.amazonaws.com'],
['eventName', 'ConsoleLogin'],
['awsRegion', 'us-east-1'],
['sourceIPAddress', '.'],
['userAgent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'],
['errorMessage', 'Failed authentication'],
['requestParameters', None],
[{"responseElements":{"ConsoleLogin":"Success"}}],
[{"additionalEventData" : ""}]
]
converted = {}
for item in x:
if isinstance(item[0], dict):
converted.update(item[0])
continue
converted[item[0]] = item[1]
for k,v in converted.items():
print(k," : ",v)
给输出
eventVersion : 1.08
userIdentity : {'type': 'IAMUser', 'principalId': '', 'accountId': '', 'accessKeyId': '', 'userName': 'test'}
eventTime : 2021-09-22T13:49:49Z
eventSource : signin.amazonaws.com
eventName : ConsoleLogin
awsRegion : us-east-1
sourceIPAddress : .
userAgent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
errorMessage : Failed authentication
requestParameters : None
responseElements : {'ConsoleLogin': 'Success'}
additionalEventData :