Python 3 Read json file with missing objects in lines

I am reading a json file with the structure below:

    [{"id":1,"gender":"Male","first_name":"Andrew","last_name":"Scott","email":"ascott0@shutterfly.com","ville":"Connecticut"},
{"id":3,"first_name":"Mary","last_name":"Richards","email":"mrichards2@japanpost.jp","ville":"Minnesota"}]

      

So, as you can see in the second line, the gender field is not present. I understand that since my code to read the file ended up being wrong on that line.

my code:

import json

def jsonreader():
##Reader for json files
    ##Open files using json library
    with open('cust_data.json') as file:
        data = json.load(file)
    resultlist = list()
    for line in data:
        print(line["id"],line["gender"])

      

I got error: -

C:/xxxxx/x.py
1 Male
Traceback (most recent call last):
2 Female
File "C:/xxxxx/x", line 67, in <module>
jsonreader()
File "C:/xxxxx/x", line 56, in jsonreader
print(line["id"],line["gender"])
KeyError: 'gender'

      

Before you answer the guys, you should know that I have a method for determining the default value in "gender", voila my method:

def definegender(x):
    if x is None:
        x = 'unknown'
        return x
    elif (x =='Male') or (x=='Female'):#not None:
        return {
         'Male':'M',
         'Female': 'F'
        }.get(x)
    else:
        return x

      

So, in this case, I couldn't use something like a default reading values ​​because I need to send some value to my method.

Some of you guys would know how best to read files like this when we are missing objects. thank

+3


source to share


3 answers


While this already has a great answer, my point is that there may be alternatives. So this is it:

for line in data:
    try:
        print(line["id"],line["gender"])
    except KeyError:
        print(line["id"],"Error!!! no gender!")

      

This is called ErrorHandling. Read the docs here: https://docs.python.org/3.6/tutorial/errors.html


update : Do you mean that? update2 bug fixed



try:
    gender = definegender(line["gender"])
except KeyError:
    gender = definegender(None)
print(line["id"],gender)

      


update3 : (for future purposes)

as.get () returns None by default the simplest solution would be

gender = definegender(line.get("gender"))
print(line["id"],gender)

      

+1


source


why not use the default for your dictionary dict.get

?

print(line["id"],line.get("gender","unknown"))

      

And since you want to transform the input even more, you can insert the two dict.get

together, the first with None

as default and a new table, for example:



gender_dict = {"Male":"M", "Female":"F", None : "unknown"}

print(line["id"],gender_dict.get(line.get("gender")))

      

(note that you no longer need your overcomplex gender method)

+6


source


Why not simplify this with an if statement?

for line in data:
    if "gender" in line:
        print(line)

      

0


source







All Articles