Python:识别文件中的时间

时间:2018-01-19 19:49:18

标签: python python-3.x

我是Python的新手,所以道歉,但是允许Python识别时间并将它们用作整数的最佳方法是什么?我有一个文件,需要计算两次之间的行数,例如每小时。该文件如下所示:

Feb  3 08:17:01 j4-be02 CRON[32735]: pam_unix(cron:session): session opened for user root by (uid=0)
Feb  3 08:17:01 j4-be02 CRON[32735]: pam_unix(cron:session): session closed for user root
Feb  3 08:35:21 j4-be02 sshd[32741]: reverse mapping checking getaddrinfo for reserve.cableplus.com.cn [211.167.103.172] failed - POSSIBLE BREAK-IN ATTEMPT!
Feb  3 08:35:21 j4-be02 sshd[32741]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=211.167.103.172  user=root
Feb  3 08:35:23 j4-be02 sshd[32741]: Failed password for root from 211.167.103.172 port 34583 ssh2
Feb  3 08:35:27 j4-be02 sshd[32744]: reverse mapping checking getaddrinfo for reserve.cableplus.com.cn [211.167.103.172] failed - POSSIBLE BREAK-IN ATTEMPT!

到目前为止,我已经成功将时间分成':'(参见下面的代码),但我不知道如何将HH或MM或SS保存为变量,以便我可以让Python知道什么时候下一个小时?例如,如果文件在08:17:01开始,我需要它在08:17:01和09:17:01之间计算文件中的行数。

  failedPass = 'Failed password for'
  for line in authStrings:
    if ":" in line and failedPass in line:
      time = line.split(':')
      print(time)

非常感谢!

1 个答案:

答案 0 :(得分:0)

首先,将字符串格式化为可用列表:

string = """Feb  3 08:17:01 j4-be02 CRON[32735]: pam_unix(cron:session): session opened for user root by (uid=0)
Feb  3 08:17:01 j4-be02 CRON[32735]: pam_unix(cron:session): session closed for user root
Feb  3 08:35:21 j4-be02 sshd[32741]: reverse mapping checking getaddrinfo for reserve.cableplus.com.cn [211.167.103.172] failed - POSSIBLE BREAK-IN ATTEMPT!
Feb  3 08:35:21 j4-be02 sshd[32741]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=211.167.103.172  user=root
Feb  3 08:35:23 j4-be02 sshd[32741]: Failed password for root from 211.167.103.172 port 34583 ssh2
Feb  3 08:35:27 j4-be02 sshd[32744]: reverse mapping checking getaddrinfo for reserve.cableplus.com.cn [211.167.103.172] failed - POSSIBLE BREAK-IN ATTEMPT!"""
times = [line.split()[2] for line in string.split('\n')]

接下来,我们将它们转换为datetime对象:

from datetime import datetime, timedelta
datetimes = [datetime.strptime(time, '%H:%M:%S') for time in times]

然后,在给定时间的情况下,我们可以确定在该时间之后的设定时段之前出现的行数。在这种情况下,我们将第一次使用作为开始时间,并使用10分钟的偏移量:

start = datetimes[0]
offset = {"minutes":10}
print(len([time for time in datetimes if time < start + timedelta(**offset)]))