考虑一个日志文件,其中包含时间戳ip和其他信息,如下所示:
22:30 1.1.1.2 buffer overflow
22:30 1.1.1.2 drops 10 packets
22:30 1.1.1.3 drops 15 packets
22:35 1.1.1.2 drops 20 packets
我想解析日志和输出:
1.1.1.2 dropped a total of 30 packets
1.1.1.3 drooped a total of 15 packets
我开始时:
f = open('log.txt', 'r')
for line in f:
if 'drops' in line:
output = line.split()[1:]
print output[1], output[3]
这会给我:
1.1.1.2 10
1.1.1.3 15
1.1.1.2 20
我不知道如何为相同的ip chk然后添加数据包。 有人可以帮忙吗? THX
答案 0 :(得分:2)
with open('log.txt', 'r') as f:
drops = {}
for line in f:
if 'drops' in line:
time, ip, fn, n, packets = line.split()
drops[ip] = drops.get(ip, 0) + int(n)
for ip, count in drops.items():
print ip, count
这会产生输出:
1.1.1.2 30
1.1.1.3 15
有关此代码的两个注意事项:
这使用了python的with
构造,以便您可以确保在不再需要该文件时将其关闭。
将数据解压缩为具有有意义名称的变量:
time, ip, fn, n, packets = line.split()
这使得后面的行更具可读性。
答案 1 :(得分:1)
收集所有ips,如dict密钥,并将数据包丢失编号添加为值
>>> ip_dict = {}
>>> with open('file.txt') as f:
... for line in f:
... if 'drops' in line:
... output = line.split()[1:]
... ip = output[0]
... packet_lost = output[2]
... if not ip_dict.get(ip,{}):
... ip_dict[ip] = 0
... ip_dict[ip] += int(packet_lost)
...
>>>
>>> ip_dict
{'1.1.1.2': 30, '1.1.1.3': 15}
然后你可以迭代并格式化输出
>>> for ip, total in ip_dict.iteritems():
... print '%s dropped a total of %i packets' % (ip,total)
...
1.1.1.2 dropped a total of 30 packets
1.1.1.3 dropped a total of 15 packets
答案 2 :(得分:1)
您可以将defaultdict用于此目的
from collections import defaultdict
d=defaultdict(int,{})
f = open('a.txt', 'r')
for line in f:
if 'drops' in line:
data=line.split()
d[data[1]]=d.setdefault(data[1], 0)+ int(data[3])
f.close()
print d
defaultdict(<type 'int'>, {'1.1.1.2': 30, '1.1.1.3': 15})
如果defaultdict过度使用,我们可以使用dict
d={}
f = open('a.txt', 'r')
for line in f:
if 'drops' in line:
data=line.split()
d[data[1]]=d.setdefault(data[1], 0)+ int(data[3])
print d