0

I have a json file which i am loading in order to filter through a certain key called "sender_id". I can seem to filter through any other keys but when it comes to filtering for "sende_id" it results in a KeyError: 'sender_id'

My python script is as follows:

import json_lines

 with open('specifications.jsonl', 'rb') as f:
    for item in json_lines.reader(f):
    print(item["sender_id"])

My jsonlines file sample as as below :

{"status": "Inactive", "no_of_rejected_submissions": 0, "name": 
 "multi senders 6", "data_type": "acute", "author": "JKS", 
"

2 Answers2

0

Use the get method to get the key to avoid the KeyError exception.

item.get('sender_id')

If the key is present it would return the value else it would return the default value which is None in this case.

You can also set the default value by using,

item.get('sender_id', 'NA')  # Here default value is NA
noufel13
  • 653
  • 4
  • 4
  • That makes sense. At the end of the loop i want to be able to count how many records have been printed out. Would that be outside of the loop – london2012_dd Aug 28 '19 at 08:34
0

As you pointed out, the first line doesn't have the key sender_id, therefore the item object would spit out KeyError when you try to access a non-exist key.

If you move the second line to the first line, then it will at least print out the result of item["sender_id"] for the line (i.e. RAK) and then spit out KeyError.

If your desired behavior is to "print if exist, skip if not exist", then you could you dict.get, for example:

with open('specifications.jsonl', 'rb') as f:
    for item in json_lines.reader(f):
        print(item.get("sender_id")) 
AnnieFromTaiwan
  • 3,845
  • 3
  • 22
  • 38
  • It does not seem to be printing out same key values. There are certain sender_id which are the send. The output will essentially be printed out to a text file – london2012_dd Aug 28 '19 at 08:56