I have a json file that look like this: I will have to extract the events eg. 'APP_STARTED' 'ORIENTATION' etc
{u'ParamElement_ReceivedTime': u'2012-11-02-00-05-31-748',
u'ParamElement_Name': u'LOG_CONTENT',
u'ParamElement_Info_0':
{u'dict':
{u'Events_list': [
{
u'Event': u'APP_STARTED',
u'time': u'2012-11-01 20:00:59.565 -0400'},
{
u'time': u'2012-11-01 20:01:01.168 -0400',
u'Event': u'ORIENTATION',
u'Orientation': u'Portrait'},
{u'Event': u'CLIENT_RESULT_RECEIVED',
u'time': u'2012-11-01 20:01:15.927 -0400'},
{u'Prev_SessionID': u'802911CC329E47139B61B58E21BF2FFF',
u'Prev_TransactionID': u'2',
u'Tab_Index': u'5',
u'time': u'2012-11-01 20:01:15.941 -0400',
u'Event': u'RESOLVED_TAB',
u'Accuracy': u'5.000000'},
{u'Prev_TransactionID': u'2',
u'Prev_SessionID': u'802911CC329E47139B61B58E21BF2FFF',
u'Event': u'CLIENT_RESULT_RECEIVED',
u'time': u'2012-11-01 20:01:16.568 -0400'}
}
The whole thing is stored in a variable called event_dict. I have a code that looks like:
if event_dict:
if 'dict' in event_dict['ParamElement_Info_0']:
if 'el' in event_dict['ParamElement_Info_0']['dict']:
if 'e' in event_dict['ParamElement_Info_0']['dict']['el']:
print e['Event']
What could be the mistake?
-
What mistake? What error or incorrect behavior are you getting?David Robinson– David Robinson2013年03月13日 16:34:46 +00:00Commented Mar 13, 2013 at 16:34
-
no error. I tried to print dict and it is empty.user1189851– user11898512013年03月13日 16:35:19 +00:00Commented Mar 13, 2013 at 16:35
3 Answers 3
Python approach is Ask forgiveness, not permission, and it is easier and better to use try-catch blocks instead of condition checks unless condition fail must be handled separately.
try:
event = event_dict['ParamElement_Info_0']['dict']['Events_list']
except Exception, e:
log('Opsss, incorrect data format: %s' % e.message)
In that way, you can see your errors easily.
Comments
You never define the variable e: your last line should be a loop, not a conditional like the earlier lines:
for e in event_dict['ParamElement_Info_0']['dict']['el']:
print e
Also, I think you say "el" when you would need to say "Events_list", making your corrected code:
if event_dict:
if 'dict' in event_dict['ParamElement_Info_0']:
if 'Events_list' in event_dict['ParamElement_Info_0']['dict']:
for e in event_dict['ParamElement_Info_0']['dict']['Events_list']:
print e
4 Comments
Events_list. You are checking whether that string is in your dictionary. You can't check for an entirely different string. Did you mean for it to be a for loop, like for el in event_dict...?There is no 'el' element in your dictionary. When you write a for A in B you are creating a variable A to hold the contents of the iterable B. What you are doing is saying, if the key 'el' is in my dictionary... which it isn't. But Events_list is as @David points out.
Here is what may be an easier approach.
def item_getter(struct, key):
parts = key.split('.', 1)
if len(parts) > 1:
key_part, rest_part = parts
return item_getter(struct.get(key_part, {}), rest_part)
return struct.get(key, None)
items = item_getter(event_dict, "ParamElement_Info_0.dict.Events_list")
events = [item.get('Event', 'No Event') for item in items]
print events
OUTPUT
[u'APP_STARTED', u'ORIENTATION', u'CLIENT_RESULT_RECEIVED', u'RESOLVED_TAB', u'CLIENT_RESULT_RECEIVED']