4

So I am reading through two json files to check if the keys file name and file size exist. In one of the files i only have the key filename and not file size. When running my script it keeps through a KeyError , i would like to instead make it print out the file name/names that do not have the key file size.

The error i get is :

if data_current['File Size'] not in data_current:
KeyError: 'File Size'


file1.json

{"File Name": "personDetails.json Exists", "File Size": "7484"}
{"File Name": "agent.json Not Exists"}

file2.json

{"File Name": "personDetails.json Exists", "File Size": "7484"}
{"File Name": "agent.json Not Exists",  "File Size": "9484"}

My code is as follows :

with open('file1.json', 'r') as f, open('file2.json', 'r') as g:

    for cd, pd in zip(f, g):

        data_current = json.loads(cd)
        data_previous = json.loads(pd)
        if data_current['File Size'] not in data_current:
            data_current['File Size'] = 0


        if data_current['File Name'] != data_previous['File Name']:  # If file names do not match
            print " File names do not match"
        elif data_current['File Name'] == data_previous['File Name']:  # If file names match
            print " File names match"
        elif data_current['File Size'] == data_previous['File Size']:  # If file sizes match
            print "File sizes match"
        elif data_current['File Size'] != data_previous['File Size']: # 


            print "File size is missing"
        else:
            print ("Everything is fine")

3 Answers 3

2

You can check if a key exists in dictionary by doing if 'File Size' not in data_current:

>>> data = {"File Size": 200} # Dictionary of one value with key "File Size"
>>> "File Size" in data # Check if key "File Size" exists in dictionary
True
>>> "File Name" in data # Check if key "File Name" exists in dictionary
False
>>>
Sign up to request clarification or add additional context in comments.

Comments

1

The if key in dict approach is likely right for you, but it's worth also knowing about the get() method of dict objects.

You can use this to attempt to retrieve a key's value from the dictionary and if it's not present it'll return a default value instead - None by default or you can specify your own value:

data = {"foo": "bar"}
fname= data.get("file_name")  # fname will be None
default_fname = data.get("file_name", "file not found")  # default_fname will be "file not found"

This can be convenient in some situations. You can also write this long hand as:

defalut_fname = data["file_name"] if "file_name" in data else "file not found" 

But I don't like having to write the key that many times!

Comments

0

Use if 'File Size' not in data_current:

When using in against a dict, python looks at keys, not at the values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.