1 回答
TA贡献1719条经验 获得超6个赞
import datetime as dt
from dateutil.parser import parse
import json
def events_equal(event1, event2):
return event1["eventTypeCode"] == event2["eventTypeCode"] and event1["region"] == event2["region"]
json_data = ### Insert your json data string here ###
parsed_json_data = json.loads(json_data)
filtered_events = []
for parsed_event in parsed_json_data["events"]:
already_present = False
## for every event, check if we've already added it to the filtered list
for filtered_event_index in range(len(filtered_events)):
if events_equal(parsed_event, filtered_events[filtered_event_index]):
already_present = True
## Convert startTime strings to datetime object for comparison
parsed_event_date = parse(parsed_event["startTime"])
filtered_event_date = parse(filtered_events[filtered_event_index]["startTime"])
## Use parse if on an older version of python, otherwise
## strptime can handle the conversion
## parsed_event_date = dt.datetime.strptime(parsed_event["startTime"], '%Y-%m-%d %H:%M:%S%z')
## filtered_event_date = dt.datetime.strptime(filtered_events[filtered_event_index]["startTime"], '%Y-%m-%d %H:%M:%S%z')
if parsed_event_date > filtered_event_date:
## if parsed event date is newer, replace the one already present
filtered_events[filtered_event_index] = parsed_event
if not already_present:
filtered_events.append(parsed_event)
## build the new json object
filtered_events = {"events": filtered_events}
print(json.dumps(filtered_events, indent=4))
输出:
{
"events": [
{
"eventTypeCode": "AWS_RDS_SECURITY_NOTIFICATION",
"startTime": "2020-01-15 11:50:00-06:00",
"region": "us-east-1"
},
{
"eventTypeCode": "AWS_TRUSTEDADVISOR_OPERATIONAL_NOTIFICATION",
"startTime": "2020-01-27 12:36:00-06:00",
"region": "us-east-1"
},
{
"eventTypeCode": "AWS_MARKETPLACE_OPERATIONAL_NOTIFICATION",
"startTime": "2019-11-11 11:30:00-06:00",
"region": "us-east-1"
},
{
"eventTypeCode": "AWS_RDS_SECURITY_NOTIFICATION",
"startTime": "2020-01-10 11:55:00-06:00",
"region": "us-west-1"
}
]
}
这仅适用于从 python 3.7 开始,早期版本的 strptime 将仅以-0600and not的形式解析 UTC 偏移量-06:00。
编辑:我用 dateutil 解析器替换了 strptime 调用,它将与早期版本的 python 一起使用。
添加回答
举报