我已经完成了一个脚本,其中在给定时间戳的情况下,它在文件中查找相同的时间戳,该文件包含以不同格式编写的不同时间戳。
这是代码:
import pytz, datetime
import re
from dateutil.parser import parse
local = pytz.timezone ("Europe/Zurich")
naive = datetime.datetime.strptime ("2018-9-13 12:46:00", "%Y-%m-%d %H:%M:%S")
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
print 'utc_dt = ', utc_dt
utc_fileFormat=utc_dt.isoformat()
print 'utc_dt.isoformat() = ', utc_fileFormat
string=parse(utc_fileFormat)
print string
new_string=string.strftime('%Y-%m-%dT%H:%M:%S')
if new_string.endswith('+00:00'):
new_string = new_string[:-6]
print new_string
with open('fill_7151_B1_timestamps.txt', 'r') as file:
for line in file:
if new_string in line:
print line
file.close()
现在我想做的是:如果我提供的时间戳记与文件中的时间戳记不完全一致,那么我必须找到更接近的时间戳记。
只是对文件中的数据有所了解,它们就像这样:
0 2018-07-30T15:20:36.524281125
1 2018-07-30T15:21:12.821653375
2 2018-07-30T15:21:29.271170975
3 2018-07-30T15:21:30.381507275
4 2018-07-30T16:13:05.816863525
5 2018-07-30T16:13:06.958256975
6 2018-07-30T16:13:08.082365950
7 2018-07-30T16:13:09.221720400
8 2018-07-30T16:13:10.359005400
9 2018-07-30T16:13:11.487593100
10 2018-07-30T16:13:12.626307750
有人可以帮助我吗?谢谢!