如何对从第2行开始的.txt文件的特定列中的值进行求和

时间:2017-04-08 17:31:33

标签: python numpy

我试图在列#34; Packets"下填写所有值。显然我只想要整数而不是字符串" Packets"所以它必须从第2行开始。在这种情况下,输出应该等于9。对于Packets的情况,其列id为[7]。

CODE:

import os
from scipy import *
from matplotlib.pyplot import *
import matplotlib.pyplot as plot
import socket

with open('/Users/rojeliomaestas/Desktop/nettest2.txt') as infile:
    next(infile)
    for line in infile:


        print("------------------------------------------------")
        print("Date:",line.split()[0])
        print("Time:",line.split()[1])
        print("Protocol:",line.split()[3])
        print("Packets:",line.split()[7])
        print("From IP:",line.split()[4])
        print("To IP:",line.split()[6])
        print("------------------------------------------------")


        data = open('/Users/rojeliomaestas/Desktop/nettest2.txt').read()


    print ("Total UDP protocols are:", data.count('UDP'))        
   # if 'UDP':    
    #    print ("~ Total ICMP packets are:", data.count(line.split()[7]))

    print ("\nTotal TCP protocols are:", data.count('TCP'))
   # if 'TCP':    
    #    print ("~ Total TCP packets are:", data.count(line.split()[7]))        

    print ("\nTotal ICMP protocols are:", data.count('ICMP'))

   # if 'ICMP':    
    #   print ("~ Total ICMP packets are:", data.count(line.split()[7]))

    val = (data.count('UDP') + data.count('TCP') + data.count('ICMP'))

    print("\nTotal protocols in the time frame",line.split()[1],"is:",val,"protocols.")





    udp = (data.count('UDP'))
    tcp = (data.count('TCP'))
    icmp = (data.count('ICMP'))


    figure(1)

    ax = plot.subplot(111)

    ax.bar(1,udp,width=0.2,align='center')
    ax.bar(2,tcp,width=0.2,align='center')
    ax.bar(3,icmp,width=0.2,align='center')
    ax.set_xticklabels(['0', 'UDP', '', 'TCP', '', 'ICMP'])
    xlabel("This graph shows amount of protocols used")
    ylabel("Number of times used")


print("\nThe amount of times The domain Northern New Mexico college was accessed is:", data.count('205.166.231')) 



domainName = socket.gethostbyaddr(line.split()[4].rsplit(':', 1)[0])[0]
if 'google' in domainName:                                  

    total = 0

with open('/Users/rojeliomaestas/Desktop/nettest2.txt') as f:
    for line in f:
        finded = line.find('8.8.8.8')
        if finded != -1 and finded != 0:
            total += 1

print("\nThe domain Google.com occurs", total,"times.")



s1 = line.split()[4][line.split()[4].find(":")+1:]  
port1 = int(s1)

portName = socket.getservbyport(port1)
if 'http' in portName:
    print("\nProtocol for port 80: HTTP")

[文字内容]

Date first seen          Duration Proto      Src IP Addr:Port          Dst IP Addr:Port   Packets    Bytes Flows
2017-04-02 12:08:32.079     9.298 UDP            8.8.8.8:80 ->     205.166.231.250:49929        1      275     1
2017-04-02 12:08:32.079     9.298 UDP            8.8.8.8:80 ->     205.166.231.250:49929        1      275     1
2017-04-02 12:08:32.079     9.298 UDP            8.8.8.8:80 ->     205.166.231.250:49929        1      275     1
2017-04-02 12:08:32.079     9.298 TCP            8.8.8.8:80 ->     205.166.231.250:49929        1      275     1
2017-04-02 12:08:32.079     9.298 TCP            8.8.8.8:80 ->     205.166.231.250:49929        1      275     1
2017-04-02 12:08:32.079     9.298 TCP            8.8.8.8:80 ->     205.166.231.250:49929        1      275     1
2017-04-02 12:08:32.079     9.298 TCP            8.8.8.8:80 ->     205.166.231.250:49929        1      275     1
2017-04-02 12:08:32.079     9.298 ICMP           8.8.8.8:23 ->     205.166.231.250:49929        1      275     1
2017-04-02 12:08:32.079     9.298 ICMP           8.8.8.8:23 ->     205.166.231.250:49929        1      275     1

[文字内容]

2 个答案:

答案 0 :(得分:1)

你非常接近你的目标。只需将每个数据包添加到变量中即可。

packets = 0
infile = open('E:\sample.txt')
for line in infile.readlines()[1:]
    packets += int(line.split()[7]) # As, packet column in 5th
print("Packets:"+str(packets))
infile.close()

它将允许您打印所有数据包。使用python 2.7

希望,这有帮助!

答案 1 :(得分:0)

sum = 0
for line in infile:
    columns = line.split() # do it once per line
    sum = sum + int(columns[7])
    ...

print(sum)

此处打印完整代码9

with open('gettingSum_Cg.txt') as infile:
    next(infile)
    sum = 0
    for line in infile:
        columns = line.split() # do it once per line
        sum = sum + int(columns[7])

print(sum)

此处的内容为gettingSum_Cg.txt

Date first seen         Duration Proto      Src       IP Addr:Port Dst IP Addr:Port       Packets Bytes Flows 
2014-04-02 12:08:32.079    9.298 UDP        8.8.8.8.:80 ->     205.166.231.250:49929      1     275    1
2014-04-02 12:08:32.079    9.298 UDP        8.8.8.8.:80 ->     205.166.231.250:49929      1     275    1
2014-04-02 12:08:32.079    9.298 UDP        8.8.8.8.:80 ->     205.166.231.250:49929      1     275    1
2014-04-02 12:08:32.079    9.298 UDP        8.8.8.8.:80 ->     205.166.231.250:49929      1     275    1
2014-04-02 12:08:32.079    9.298 UDP        8.8.8.8.:80 ->     205.166.231.250:49929      1     275    1
2014-04-02 12:08:32.079    9.298 UDP        8.8.8.8.:80 ->     205.166.231.250:49929      1     275    1
2014-04-02 12:08:32.079    9.298 UDP        8.8.8.8.:80 ->     205.166.231.250:49929      1     275    1
2014-04-02 12:08:32.079    9.298 UDP        8.8.8.8.:80 ->     205.166.231.250:49929      1     275    1
2014-04-02 12:08:32.079    9.298 UDP        8.8.8.8.:80 ->     205.166.231.250:49929      1     275    1

运行时的代码打印 9 ...