Python Forum

Full Version: How to express null value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, problem i have is i am downloading a json file and putting in a string, when i am getting data from the string at times there is some info that is not in the json file because ii is just not available, i need to replace this blank info with a zero so when i process all the other data i don't get errors, he is some simple code where i add all the data from that item to a list.

laststart = previous[0]["handicap"]
weightchange += [laststart]

the variable laststart should contain the output of "handicap" but when "handicap" is not in the json file this is missing from my list, as an example the list should contain 14 items but only has 13
this looks like it solved the issue.


        try:
            laststart = previous[0]["handicap"]
            weightchange += [laststart]
        except:
            laststart = 0
            weightchange += [laststart]
            pass
Dictionaries also have a get method that lets you specify a default value if the key is missing: https://docs.python.org/3/library/stdtyp...l#dict.get.
cheers