import pandas as pd
import json
mass=[]
fall=[]
year=[]
req = requests.get("https://data.nasa.gov/resource/y77d-th95.json")
response =req.json()
for i in range(0,len(response)):
mass.append(response[i]['mass'])
fall.append(response[i]['fall'])
year.append(response[i]['year'])
I handle keyError with the help of Exception handling. If i got keyerror i added NAN value.
As java have function like OptString whenever we get keyerror it add default value.
I just want to get all data in list but via using optString type method.
Can you give any example ?
Thanks ,
>Solution :
You can use dict.get to provide a default value as the second argument.
For example:
from math import nan
for obj in response:
mass.append(obj.get('mass', nan))
fall.append(obj.get('fall', nan))
year.append(obj.get('year', nan))