I am new to coding and I was creating a metaData conversion function the format for the new file has a date and time stamp in this format yyyymmdd_hhmmss. I created the following code with a series of if statements because the original metaData had the day/month listed as a single digit (double when necessary. I assume there is a more elegant way to accomplish this task and for learning purposes and readability/simplicity. Any suggestions would be appreciated! Any other suggestions for simplification of the code is also appreciated, thanks.
for fileExt in fpath:
with open(fileExt) as f:
data = json.loads(f.read())
if 'animalID' in data:
ext=fileExt.replace('\\metaData.json', '\\Miniscope\\metaData.json')
animalID=data['animalID']
timeStamp=data['recordingStartTime']
year=str(timeStamp['year'])
month=str(timeStamp['month'])
if len(month) < 2:
month=str(0)+month
day=str(timeStamp['day'])
if len(day) < 2:
day=str(0)+day
second=str(timeStamp['second'])
if len(second) < 2:
second=str(0)+second
minute=str(timeStamp['minute'])
if len(minute) < 2:
minute=str(0)+minute
hour=str(timeStamp['hour'])
if len(hour) < 2:
hour=str(0)+hour
date=year+month+day+'_'+hour+minute+second
with open(ext) as d:
data2 = json.loads(d.read())
frameRate=float(data2['frameRate'])
jdict ={'origin':animalID,'fps':frameRate,'date':date,
'orig_meta':[data,data2]}
jsonFile=json.dumps(jdict,indent=4)
newFileName=ext.replace('\\metaData.json', '\\metaDataTif.json')
n = open(newFileName, 'w')
n.write(jsonFile)
n.close()