JSON format:
[{"SH_MSG": {"time": "1657291114000", "area_id": "D1", "address": "54", "msg_type": "SH", "data": "8CFB0B00"}}, {"SF_MSG": {"time": "1657291114000", "area_id": "D2", "address": "0A", "msg_type": "SF", "data": "1F"}}, ...}][...]
I want to record all data that has a CA_MSG
tag at the start.
I am using stomp to obtain messages.
msg = json.loads(frame.body)
msg
is a list such that:
msg = [{'SF_MSG': {'...'}}, ...]
I am trying:
for m in msg:
new_msg = []
if m.keys() == 'CA_MSG':
new_msg.append(m)
But this is just returning []
every time.
CodePudding user response:
I ended up getting there in the end by skipping out the for loops for a list comprehension:
CA_MSGS = [msg['CA_MSG'] for msg in message if 'CA_MSG' in list(msg.keys())]
CodePudding user response:
dict.keys() returns a list object, it can never be a True if you check the list object with a string object
if m.keys() == 'CA_MSG': # False all the time
# probably this is what you are looking for
if m.keys().count('CA_MSG') > 0: