2017-09-13 99 views
-2

我想知道如何從GeoIP代碼中統計所列國家的總數。我已經做了一個代碼,但它只顯示了總數的一半,例如它表明中國出現了65次,但還不止於此。這是我的代碼我如何計算我的國家在python中的GeoIP總數

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

@koalo我有txt文件,但它太大了 – Angeline

+0

舉一個例子,從它的幾行。 – Igle

+0

@Igle我已更新我的文章,請檢查 – Angeline

回答

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) 
相關問題