I have a log-file where every line contains IP address, time of access, and the URL accessed. I want to count the accesses per hour.
Time of access data looks like 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 I improve it so I don't need to set up the variable and if statement for 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
You can include the brackets into your strptime
format description:
datetime.datetime.strptime(line.strip(),'[%d/%b/%Y:%H:%M:%S %z]')
You can extract the hour using the .hour
attribute of any datetime.datetime
object:
timestamp = datetime.datetime.strptime(…)
hour = timestamp.hour
You can count the number of elements using a collections.Counter
:
from collections import Counter
def read_logs(filename):
with open(filename) as log_file:
for 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/'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With