I have this piece of code (below). I have data in json file and I fetch the data (code below). I zip these values and write them to csv file.
My issue is that if any of the value doesn't exist in try
I want to put a 'None' over there for that value. What I tried (I think is lame) -> I added try and except for each values that I am fetching below. I need help in writing this better.
The Exception tells me what value is missing. For example, code
is missing. So how to I deal with this in except
and put None
for code
?
for i in range(len(res_1)):
try:
gp_code.append(res_1[i]['metadata']['annotations']['code'])
ticket.append(res_1[i]['metadata']['annotations']['ticket'])
node.append(res_1[i]['metadata']['annotations']['node-selector'])
display_name.append(res_1[i]['metadata']['name'])
status.append(res_1[i]['status']['phase'])
timer.append(res_1[i]['metadata']['creationTimestamp'])
lifetime.append(res_1[i]['metadata']['annotations']['lifetime'])
except Exception as e:
print(e)