2017-09-13 9 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] 

これであるので、中国1、中国2、中国2 は、だから私は出力をしたいがある

China       1 
China       2 
Ireland      1 
China       3 
Moldova, Republic of   1 
Japan       1 
China       1 
China       2 
Brazil      1 

私の出力です:

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
はあなたがカウントしたいIPの一覧です。

import collections 
c = collections.Counter() 
for ip in match_pattern: 
    c.update(get_country_from_ip(ip)) 

print(c) 
関連する問題