I have the following code which uses an API to download data from a website.
url = "https://gtr.ukri.org/project"
params = {"page": 11,
"fetchSize": 100}
projects = requests.get(url,
params=params,
headers={"accept": "application/json"}).json()['projectsBean']['project']
for project in projects:
list_projects.append({'ref': project['grantReference'], 'funder': project['fund']['funder']['name'],
'title': project['title'], 'category': project['grantCategory'],
'abstract': project['abstractText'], 'funding': project['fund']['valuePounds'],
'start': project['fund']['start'], 'end': project['fund']['end']})
This is working fine, however sometimes there is no value for a key when iterating through all the pages in the website (the key will always exist).
When this happens, it errors as a value is not there.
How do I append a dictionary to a list and just leave any values for any key blank if it does not exist, rather than erroring?
Many thanks
>Solution :
To have a default value when no value exists in your dictionary you can do it like this
project.get('grantReference', 'no-ref')
the string 'no-ref' is a default value if the project dict doesn’t contain any key named grantReference.
the above is only an example to do get a value of dict with key without error.
but however, on your question
This is working fine, however sometimes there is no value for a key when iterating through all the pages in the website (the key will always exist).
you doesn’t mention the exact code line where the error come from so I can not help to create a complete example code.
For nested dictionary you can use the method like this
project.get('fund', {}).get('start', 0)
the first key 'fund' will return value that you expecting or will default to be an empty dictionary. so the second key 'start' will get value with key 'start' from the dictionary you expecting or from the default empty dictionary we provide. (and we provide default value for key 'start' which is the 0)