简化一系列 if 语句
Simplify a series of if statements
我是编码新手,我正在创建一个元数据转换函数,新文件的格式具有这种格式的日期和时间戳 yyyymmdd_hhmmss。我使用一系列 if 语句创建了以下代码,因为原始元数据将 day/month 列为单个数字(必要时加倍。我认为有更优雅的方法来完成此任务并用于学习目的和 readability/simplicity。任何建议将不胜感激!任何其他简化代码的建议也将不胜感激,谢谢。
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()
这是您可以使用的解决方案
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()
我是编码新手,我正在创建一个元数据转换函数,新文件的格式具有这种格式的日期和时间戳 yyyymmdd_hhmmss。我使用一系列 if 语句创建了以下代码,因为原始元数据将 day/month 列为单个数字(必要时加倍。我认为有更优雅的方法来完成此任务并用于学习目的和 readability/simplicity。任何建议将不胜感激!任何其他简化代码的建议也将不胜感激,谢谢。
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()
这是您可以使用的解决方案
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()