[英]Convert String to Date row by row in a file with Python
I am new at python so I have question about it.我是 Python 新手,所以我对此有疑问。 There is a file "x.list" in /data01/test directory.
/data01/test 目录中有一个文件“x.list”。 And it is linux OS.
它是linux操作系统。
This file contains;该文件包含;
"A2","Start Time; 2019-01-10T00:00:18Z, EndTime;2019-01-10T00:13:57Z"
"A3","Start Time; 2019-01-10T00:57:26Z, EndTime;2019-01-10T00:59:26Z"
"A4","Start Time; 2019-01-10T00:14:29Z, EndTime;2019-01-10T00:22:29Z
what I try to make is;我试图做的是;
"A2","2019-01-10 00:13:57, 00:13:39
"A3","2019-01-10 00:59:49, 00:2
"A4","2019-01-10 00:22:13, 00:8
Third column should be the Start time- End time第三列应该是开始时间 - 结束时间
Make use of regex and datetime
:使用正则表达式和
datetime
:
import re
from datetime import datetime
start, end = re.findall(r'(\d+-\d+-\d+)T(\d+:\d+:\d+)Z', line)
start_time = datetime.striptime(' '.join(start), '%Y-%m-%d %H:%M:%S')
end_time = datetime.striptime(' '.join(end), '%Y-%m-%d %H:%M:%S')
print(start_time.strftime('%Y-%m-%d %H:%M:%S'))
print(end_time.strftime('%Y-%m-%d %H:%M:%S'))
# delta time can be found like this
# format as you need, see above for example
delta = end_time - start_time
print(delta)
Reference:参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.