总结python输出中显示的字符串总数
例如,我想显示Dest端口号53出现了多少次,并且日志文件中有2000个数据,所以我需要显示每个Dest端口的总和。这是我的代码:总结python输出中显示的字符串总数,python,log-analysis,Python,Log Analysis,例如,我想显示Dest端口号53出现了多少次,并且日志文件中有2000个数据,所以我需要显示每个Dest端口的总和。这是我的代码: def main(): f = openfile("/Users/rin/Desktop/new sec/2017-04-18_010.082.012.003.txt") if f is None: print("File not found") return s = splitline(f) for
def main():
f = openfile("/Users/rin/Desktop/new sec/2017-04-18_010.082.012.003.txt")
if f is None:
print("File not found")
return
s = splitline(f)
for el in s:
if len(el) > 50:
p = parselog(el)
if "dstport" in p:
print("Dest Port : %s" % p["dstport"])
if "app" in p:
print("Apps : %s" % p["app"])
print("")
输出:
Dest Port : 53
Apps : DNS
Dest Port : 123
Apps : NTP
Dest Port : 53
Apps : DNS
Dest Port : 53
Apps : DNS
就像我有2000个这样的端口,那么我怎样才能一个接一个地进行输出呢?@warezers更新了,但不确定python3的语法
def main():
f = openfile("/Users/rin/Desktop/new sec/2017-04-18_010.082.012.003.txt")
if f is None:
print("File not found")
return
s = splitline(f)
# add a counter
counts = {}
for el in s:
if len(el) > 50:
p = parselog(el)
if "dstport" in p:
# increment counter
if p["dstport"] in counts:
counts[str(p["dstport"])] += 1
else:
counts[str(p["dstport"])] = 1
print("Dest Port : %s" % p["dstport"])
if "app" in p:
print("Apps : %s" % p["app"])
print("")
# output the count
for k, v in counts.iteritems():
print 'Dest Port %s Count: %s' % (k, v)