log 日志内容如下
ip地址为要访问的地址
- 2017-9-10 11:10 192.168.1.1
- 2017-9-10 11:11 192.168.1.1
- 2017-9-10 11:12 192.168.1.1
- 2017-9-10 11:11 192.168.1.2
- 2017-9-10 11:13 192.168.1.1
- 2017-9-10 11:12 192.168.1.2
- 2017-9-10 11:12 192.168.1.2
- 2017-9-10 11:11 192.168.1.1
求每分钟某个地址的访问量
d = {}
with open('log1', 'r') as f:
for line in f:
list_new = line.strip("
").split()
if list_new[1] in d:
d[list_new[1]].append(list_new[2])
else:
d[list_new[1]] = [list_new[2]]
def count(d):
for key in sorted(d):
print('''
%s
------------------''' % key)
for i in set(d[key]):
print(i,'----',d[key].count(i),'
------------------')
count(d)
结果
11:10
------------------
192.168.1.1 ---- 1
------------------
11:11
------------------
192.168.1.1 ---- 2
------------------
192.168.1.2 ---- 1
------------------
11:12
------------------
192.168.1.1 ---- 1
------------------
192.168.1.2 ---- 2
------------------
11:13
------------------
192.168.1.1 ---- 1
------------------