1

I have the following list of dictionary:

 mydata = [ { "created_time": "2017-07-22T19:54:03+0000", "message": "AAAAAAA", "id": "1892434161030557_1945301442410495" }, { "created_time": "2017-07-16T12:55:37+0000", "message": "YYYYYYYYY", "id": "1892434161030557_1941921866081786" }, { "created_time": "2017-07-16T12:43:44+0000", "message": "PPPPPPPPPPPPP", "id": "1892434161030557_1941917586082214" }, { "created_time": "2017-05-12T05:42:58+0000", "message": "m", "id": "1892434161030557_1906744326266207" } ] 

When I print the created_time it works fine:

for x in mydata: print(x['created_time']) 

I get correct output for the created_time and id values. But when I try to read the message value, I get KeyError: 'message'.

8
  • 1
    Can you show the line that throws the exception? It's clear what you are asking, but you could have a typo in there. Commented Jul 22, 2017 at 20:52
  • Are you sure that the x even has a "message" key? Commented Jul 22, 2017 at 20:52
  • @IgnacioVazquez-Abrams By looking at OP's data, it looks he has reasons to be sure of that. Commented Jul 22, 2017 at 20:54
  • 1
    Obviously this is dummy data. The actual data would be larger, with issues and missing keys in all probability. Commented Jul 22, 2017 at 20:56
  • 1
    I didn't post the complete list. In my complete list some dictionaries have message value and others don't have it and others have story instead of message. Should this be the cause? Commented Jul 22, 2017 at 21:03

2 Answers 2

4

Given your example data, this simple operation should just work. I guess that message is not there for some instances.

You can more easily debug this like this:

for x in mydata: try: msg = x['message'] except KeyError: raise ValueError('No "message" key in "%s"' % (x, )) print(msg) 

This will give you the whole instance of x that has no message.

Sign up to request clarification or add additional context in comments.

5 Comments

Why are you raising a ValueError here?
I indeed thought of editing the answer and re-raising a KeyError. However given that the "message"-key is always expected to be there, we raise a ValueError indicating that an invalid value for mydata was passed: The error is with the value.
I want to work with all the instance that have message in it. @JRG solution is a good one, since it checks if the value exists and by this ways it bypass the KeyError.
Then you should still use a try: ... except: continue or performance will hurt.
1

If you know all the possible keys in your data and do not want to use try...except then you can check the key if it exists.

One more variation would be print the key as EMPTY in else part of all if statements so you would know how many dataset didnt have any value for the expected keys.

mydata = [ { "created_time": "2017-07-22T19:54:03+0000", "message": "AAAAAAA", "id": "1892434161030557_1945301442410495" }, { "message": "YYYYYYYYY", "id": "1892434161030557_1941921866081786" }, { "created_time": "2017-07-16T12:43:44+0000", "message": "PPPPPPPPPPPPP", "id": "1892434161030557_1941917586082214" }, { "created_time": "2017-05-12T05:42:58+0000", "message": "m", "id": "1892434161030557_1906744326266207" } ] for x in mydata: if ('created_time' in x): print("created_time : " + x['created_time']) if ('message' in x): print("message : "+ x['message']) if ('id' in x): print("id : " + x['id']) print("\n") 

Sample Run

created_time : 2017-07-22T19:54:03+0000 message : AAAAAAA id : 1892434161030557_1945301442410495 message : YYYYYYYYY id : 1892434161030557_1941921866081786 created_time : 2017-07-16T12:43:44+0000 message : PPPPPPPPPPPPP id : 1892434161030557_1941917586082214 created_time : 2017-05-12T05:42:58+0000 message : m id : 1892434161030557_1906744326266207 

2 Comments

When you know your data and are checking for the data before accessing it in an object, why is solution down voted?
There is no try...catch or if..then in Python. Your code changes the logic of the program by making what once was a fatal error a silent error. Your code pays the extra cost of checking for membership three times for every row, while we can expect that check to be redundant in almost all cases.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.