-1

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()
  • [codereview.se] would be more appropriate for this. SO is not the right place. – Mad Physicist Jun 17 '21 at 20:41
  • All you need to do to zero-pad an integer `v` to 2 digits is `f"{v:02}"`. This is equivalent to `"{:02}".format(v)`, or `"%02d" % v`. – Tom Karzes Jun 17 '21 at 20:45
  • Thanks, that helps, I didn't even know a good search term to use to find an answer. – katherinekelly2012 Jun 17 '21 at 21:01
  • You can combine them too. For instance, to obtain MM/DD/YYYY, you can do `f"{month:02}/{day:02}/{year:04}"`, or `"{:02}/{:02}/{:04}".format(month, day, year)`, or `"%02/%02/%04" % (month, day, year)` – Tom Karzes Jun 17 '21 at 21:15

1 Answers1

-1

Here is a solution you could use


def adjust_value(value):
    if len(value) < 2:
        value = str(0) + value
    return value

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'])

            month = adjust_value(month)
            day=str(timeStamp['day'])

            day = adjust_value(day)
            second=str(timeStamp['second'])

            second = adjust_value(second)
            minute=str(timeStamp['minute'])

            minute = adjust_value(minute)
            hour=str(timeStamp['hour'])

            hour = adjust_value(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()
Andrew Clark
  • 545
  • 3
  • 11