- 论坛徽章:
- 0
|
本帖最后由 jzsjm1002 于 2020-06-24 18:15 编辑
- #!/usr/bin/python3
- import datetime
- # 读取文本
- def readText(path):
- with open(path, 'r') as f:
- for line in f:
- date1 = '{} {}'.format(line.split()[0], line.split()[1])
- date2 = '{} {}'.format(line.split()[2], line.split()[3])
- output = '{} {}'.format(line.strip(), handleTime(date1, date2))
- print(output)
- # 时间处理
- def handleTime(date1, date2):
- d1 = datetime.datetime.strptime(date1, '%m/%d/%Y %H:%M:%S')
- d2 = datetime.datetime.strptime(date2, '%m/%d/%Y %H:%M:%S')
- result = (d2 - d1).seconds
- return convertTime(result)
- # 时间转换
- def convertTime(result):
- if result >= 60:
- minute, seconds = divmod(result, 60)
- if minute >= 60:
- hour, minute = divmod(minute, 60)
- return convertOutput(hour, minute, 'hour')
- else:
- return convertOutput(minute, seconds, 'minute')
- else:
- return '--- {} seconds'.format(result)
- # 后缀时间差输出
- def convertOutput(arg1, arg2, type):
- if type == 'hour':
- if arg1 == 1 and arg2 == 0:
- return '--- {} hr'.format(arg1)
- elif arg1 > 1 and arg2 != 0:
- return '--- {} hrs {} minutes'.format(arg1, arg2)
- elif arg1 > 1 and arg2 == 0:
- return '--- {} hrs'.format(arg1)
- elif type == 'minute':
- if arg1 >= 1:
- return '--- {} minutes'.format(arg1)
- def main():
- # 文本路径
- path = '/mnt/test/file'
- readText(path)
- if __name__ == '__main__':
- main()
复制代码 |
|