Python Forum
How to express null value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to express null value (/thread-39677.html)



How to express null value - klatlap - Mar-25-2023

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


RE: How to express null value - klatlap - Mar-25-2023

this looks like it solved the issue.


        try:
            laststart = previous[0]["handicap"]
            weightchange += [laststart]
        except:
            laststart = 0
            weightchange += [laststart]
            pass



RE: How to express null value - ndc85430 - Mar-25-2023

Dictionaries also have a get method that lets you specify a default value if the key is missing: https://docs.python.org/3/library/stdtypes.html#dict.get.


RE: How to express null value - klatlap - Mar-25-2023

cheers