0
strStr = ["192.168.42.12", "192.168.42.2"]
with open(datausage) as f:
    lines = f.readlines()
    for line in lines:
        for ii in strStr:
            if ii in line:
                result = line
                ip = line[5:-50]
                result_ip = ip.replace(" ", "")
                usage = line[-8:]
                d = usage.replace('KB', '')
                usage = d.replace('B', '')
                usage = usage.replace('\n', '')
                print result_ip + '\t\t\t' + str(usage)

result for above code: IP usage

192.168.42.12             151
192.168.42.12            4.95
192.168.42.12            3.25
192.168.42.2             3.73
192.168.42.2             3.73
192.168.42.12            5.36
192.168.42.12              705
192.168.42.12              282
192.168.42.12              225
192.168.42.2                81
192.168.42.2                40

Desired/expected output :

Need to just display only two IP address and its sum of usage like this

192.168.42.12      1025(sample)
192.168.42.2       540(sample)

Any help! Thanks in advance!

praveen
  • 91
  • 11
  • Possible duplicate of [python word count from a txt file program](https://stackoverflow.com/questions/21107505/python-word-count-from-a-txt-file-program) – Drag and Drop Jun 02 '17 at 08:24

4 Answers4

0

Use a dictionary to store the cumulative sum for the corresponding ips:

You can store the ips as:

result_count = {}


with open(ipfile) as f:
    lines = f.readlines()
    for line in lines:
        ip = line.replace('\n', '').replace(' ', '')
        result_count[ip] = 0.0



with open(datausage) as f:
    lines = f.readlines()
    for line in lines:
        for ii in result_count:
            if ii in line:
                result = line
                ip = line[5:-50]
                result_ip = ip.replace(" ", "")
                usage = line[-8:]
                d = usage.replace('KB', '')
                usage = d.replace('B', '')
                usage = usage.replace('\n', '')
                usage = usage.replace(' ', '')
                usage = float(usage)
                # add the sum to to the related ip
                result_count[result_ip] += usage
                print result_ip + '\t' + str(usage)

for key, value in result_count.items():
     print(key, value)
0

You can try this code.

datausage = r"e:\temp\111.txt"
strStr = ["192.168.42.12", "192.168.42.2"]

with open(datausage) as f:
    lines = f.readlines()
    for ii in strStr:
        sumResult = 0
        for line in lines:
            if ii in line:
                result = line
                ip = line[5:-50]
                result_ip = ip.replace(" ", "")
                usage = line[-8:]
                d = usage.replace('KB', '')
                usage = d.replace('B', '')
                usage = usage.replace('\n', '')
                sumResult += float(usage)
        print(result_ip + '\t\t\t' + str(sumResult))

The code output is :

192.168.42.12           318.4
192.168.42.2            14.92
youDaily
  • 1,372
  • 13
  • 21
  • This is the output which i got **192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.2 3.73 192.168.42.2 3.73 192.168.42.2 0 192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.12 0 192.168.42.2 81.0 192.168.42.2 0** please copy this output in notepad++ to have a formatted look all 192.168.42.12 shows only 0 – praveen Jun 02 '17 at 08:37
  • please check the parse condition. – youDaily Jun 02 '17 at 08:52
  • sorry i am not quite sure about it. Where exactly you are asking me to check ? Only if i resolved this problem is done . Thanks in advance – praveen Jun 02 '17 at 09:09
0

you can create a new helper function that takes the output and returns the unique items with their corresponding sum:

    def getunique(data):
        newdata = []
        uniq = list(set(x[0] for x in data)
        for value in uniq:
            sum = 0.0
            for subvalue in data
                sum += data[1]
            newdata.append([uniq,sum])
        return newdata

this function assumes that the items you want to get unique values for are given in the following format:

    [[ip, valuetosum],[ip,valuetosum],[ip,valuetosum]...]

so you may have to adjust your code for that

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62
0

How about some simple logic like this? Will this work for you @praveen?

out = {}
with open(datausage) as f:
    for line in f:
        ip, count = line.split()
        out[ip] = out.get(ip, 0) + float(count)

for ip in out:
     print ip, '\t', out[ip]
Ritwik G
  • 406
  • 2
  • 8