简体   繁体   中英

KeyError when reading a list of dictionary

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' .

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM