我在编写下一个代码时遇到了一个问题:
from datetime import datetime, timedelta
def find_last_index(file_rec):
time = datetime.now() - timedelta(hours=2)
file_content = file_rec
while True:
ind = file_content.find(time.strftime("%m-%d"))
date_obj = datetime.strptime(file_content[ind:13], '%m-%d %H:%M:%S')
if time.hour > date_obj.hour:
file_content = file_content[ind+5:]
ind = file_content.find("12-22", ind)
return ind
else:
file_content = file_content[ind + 1:]
file_name = raw_input("Enter File Path From this file's dir: ")
read_file = open(file_name, 'r')
content = read_file.read()
read_file.close()
lastindex = find_last_index(content)
print content[:lastindex]
content = input()
write_file = open("ResultFile.txt", "w")
write_file.write(content[:lastindex])
write_file.close()
代码应该采用类似的日志文件:
12-22 20:14:15.972 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.972 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.972 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.972 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.973 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.974 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.974 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.974 26560 27796 D Robocol : no packet received: NullPointerException
12-22 20:14:15.9
每行以日期和时间开头。我想在2个小时前直到当前时间的语句中插入一个新文件。 如果有人帮我解决它会很棒。
答案 0 :(得分:1)
我无法抗拒建议使用箭头模块来操纵日期。在许多情况下,它使生活更轻松。这是我提供的。
>>> import arrow
>>> refTime = arrow.now().shift(hours=-2).strftime('%m-%d %H:%M:%S')
>>> refTime
'12-27 13:13:57'
>>> str(refTime)
'12-27 13:13:57'
>>> refTime_as_str = str(refTime)
>>> with open('logfile.txt') as log:
... for line in log:
... if line[:len(refTime_as_str )] >= refTime_as_str:
... print (line.strip())
...
12-27 15:45:50.972 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.972 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.972 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.972 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.973 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.974 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.974 26560 27796 D Robocol : no packet received: NullPointerException
12-27 15:45:50.974 26560 27796 D Robocol : no packet received: NullPointerException
答案 1 :(得分:0)
我相信我通过逆向工程代码逻辑找到了它。
您的函数 find_last_index 会不断更新其输入文件的本地副本。最终,它返回第一个条目的索引,该索引至少有两个(截断的)小时 - 但它是该本地副本中的索引 。在你找到那个时间之前,你要砍掉5个字符的日期,然后砍掉单个字符,这样你最终得到的索引不到20个。
回到主程序中,您仍然拥有原始数据,保存在变量内容中。您应用从例程返回的 ind 索引,该索引不再适用于内容。
如果您想保留当前的逻辑流程,那么
可能的代码更改:
ind = file_content[ind:].find(time.strftime("%m-%d"))
date_obj = datetime.strptime(file_content[ind:ind+13], '%m-%d %H:%M:%S')