1
I have the code below that plays values inside keys in a dictionary. However, I need several try
/ except
so that the code works perfectly, since, in the absence of one of the fields, the code will return a Keyerror Exception and the code will break.
Is there a more optimized and/or simpler way to solve the problem?
Code:
issue_fmt = {}
issue_fmt['assignee'] = {}
issue_fmt['assignee']['display_name'] = ''
issue_fmt['reporter'] = {}
issue_fmt['reporter']['display_name'] = ''
issue_fmt['key'] = ''
issue_fmt['project'] = {}
issue_fmt['project']['name'] = ''
def format_doc(issue):
try:
issue_fmt['assignee']['display_name'] = issue['assignee']['display_name']
except:
issue_fmt['assignee']['display_name'] = ''
try:
issue_fmt['reporter']['display_name'] = issue['reporter']['display_name']
except:
issue_fmt['reporter']['display_name'] = ''
try:
issue_fmt['key'] = issue['key']
except:
issue_fmt['key'] = ''
try:
issue_fmt['description'] = issue['description']
except:
issue_fmt['description'] = ''
try:
issue_fmt['project']['name'] = issue['project']['name']
except:
issue_fmt['project']['name'] = ''
return dict(issue_fmt)
Perfect, the get method solved my problem. Thanks! issue_fmt is set out just to simplify the example. Actually, it is read from another file. On the return, really is an unnecessary redundancy, I just fixed.
– Guilherme Carvalho Lithg