python - How to count accesses per hour from log file entries? -




i have log-file every line contains ip address, time of access, , url accessed. want count accesses per hour.

time of access data looks this

[01/jan/2017:14:15:45 +1000] [01/jan/2017:14:15:45 +1000] [01/jan/2017:15:16:05 +1000] [01/jan/2017:16:16:05 +1000]  

how can improve don't need set variable , if statement every hour?

twopm = 0 thrpm = 0 foupm = 0 timestamp = line.split('[')[1].split(']')[0] formated_timestamp = datetime.datetime.strptime(timestamp,'%d/%b/%y:%h:%m:%s %z').strftime('%h') if formated_timestamp == '14':     twopm +=1 if formated_timestamp == '15':     thrpm +=1 if formated_timestamp == '16':     foupm +=1 

  1. you can include brackets strptime format description:

    datetime.datetime.strptime(line.strip(),'[%d/%b/%y:%h:%m:%s %z]') 
  2. you can extract hour using .hour attribute of datetime.datetime object:

    timestamp = datetime.datetime.strptime(…) hour = timestamp.hour 
  3. you can count number of elements using collections.counter:

    from collections import counter   def read_logs(filename):     open(filename) log_file:          line in log_file:              timestamp = datetime.datetime.strptime(                      line.strip(),                      '[%d/%b/%y:%h:%m:%s %z]')              yield timestamp.hour   def count_access(log_filename):     return counter(read_logs(log_filename))   if __name__ == '__main__':     print(count_access('/path/to/logs/')) 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -