我想知道如何从GeoIP代码中计算所列国家/地区的总数。我已经制作了一个代码,但它只显示了总数的一半,例如它显示中国出现了65次,但还有更多。这是我的代码
import re
import string
frequency = {}
document_text = open('/Users/mani/Desktop/finalgeoip.txt', 'r')
text_string = document_text.read().lower()
match_pattern = re.findall(r'[a-z]{3,15}', text_string)
for word in match_pattern:
count = frequency.get(word, 0)
frequency[word] = count + 1
frequency_list = frequency.keys()
for words in frequency_list:
print words, frequency[words]
这是我的输出
China 1
China 2
Ireland 1
China 3
Moldova, Republic of 1
Japan 1
China 1
China 2
Brazil 1
所以有中国1,中国2,中国2 所以我想要输出:
China 5
但我的代码只计算字符串总数
这是我的日志文件
2017-04-18 00:00:00 Local7.Info 10.82.12.3 date=2017-04-17 time=23:59:59 devname=IDS-DC14-001 devid=FGT90D3Z15018997 logid=1059028704 type=utm subtype=app-ctrl eventtype=app-ctrl-all level=information vd=root appid=16206 user="" srcip=180.16.170.129 srcport=0 srcintf="wan1" dstip=116.238.73.58 dstport=771 dstintf="wan1" profiletype="applist" proto=1 service="icmp/3/3" policyid=3 sessionid=41936599 applist="sniffer-profile" appcat="Network.Service" app="ICMP" action=pass msg="Network.Service: ICMP," apprisk=elevated
2017-04-18 00:00:00 Local7.Info 10.82.12.3 date=2017-04-17 time=23:59:59 devname=IDS-DC14-001 devid=FGT90D3Z15018997 logid=1059028704 type=utm subtype=app-ctrl eventtype=app-ctrl-all level=information vd=root appid=27946 user="" srcip=10.80.10.249 srcport=9207 srcintf="wan1" dstip=208.91.112.196 dstport=53 dstintf="wan1" profiletype="applist" proto=17 service="DNS" policyid=3 sessionid=41936600 applist="sniffer-profile" appcat="Cloud.IT" app="Fortiguard.Search" action=pass msg="Cloud.IT: Fortiguard.Search," apprisk=medium
答案 0 :(得分:0)
使用collections.Counter
假设match_pattern
是您想要计算的IP列表。
import collections
c = collections.Counter()
for ip in match_pattern:
c.update(get_country_from_ip(ip))
print(c)