I get data in JSON format from one API. I need to extract specific fields and insert them into a MySQL table. Before inserting them I'm doing some changes, for example :
for record in api_data:
col1 = record["field1"][0]
col2 = int(record["field2"][:4])
col3 = ';'.join(record["field3"])
col4 = datetime.strptime(record["field4"][:10],'%Y-%m-%d').strftime('%d %b %Y')
col5 = ' '.join(record["field5"])
Some of these fields can be empty and that's why a need a try block. Is there a more elegant way than using try except block for each field individually?
I checked this question : A DRY approach to Python try-except blocks? but it didn't help me.