2017-09-05 17 views
-3

たとえば、Destポート番号53が表示され、ログファイルには2000個のデータがあることを示したいので、各Destを表示する必要がありますポート合計。これは私のコードです:Pythonの出力に現れる合計文字列を集計するには

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 

答えて

0
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) 
+0

私はこのような2000ポートを持っているので、どのように私は出力を行うために一つ一つを取ることができ、気に入りましたか? – warezers

+0

@warezersが更新されましたが、Python 3の構文についてはわかりません – bphi

関連する問題