Home > Mobile >  How to sum identical strings and sort them from txt file in python
How to sum identical strings and sort them from txt file in python

Time:09-14

I have a txt file(named file.txt) that contains two columns(ip-address and bytes, [,,]-separator)

file = open(file='file.txt')

print(file.read())

output:
13.107.4.52,,323
184.51.238.190,,505
184.51.238.190,,505
96.17.7.76,,1066
185.48.81.253,,1061
40.127.240.158,,1443
13.107.42.16,,1544
20.190.160.15,,6342
20.86.249.62,,2700
52.109.12.19,,1435
52.109.12.19,,1435
184.51.238.190,,425
40.127.240.158,,1978
20.73.130.64,,2674
204.79.197.200,,943
204.79.197.200,,943
204.79.197.200,,776

I need to combine lines with duplicate ip-addresses and sum bytes, sorted by bytes. The same thing like excels's "Consolidate data in multiple worksheets"

I need to get this:

20.190.160.15,,6342
40.127.240.158,,3421
52.109.12.19,,2870
20.86.249.62,,2700
20.73.130.64,,2674
204.79.197.200,,2662
13.107.42.16,,1544
184.51.238.190,,1435
96.17.7.76,,1066
185.48.81.253,,1061
13.107.4.52,,323

I really don't understand how to do this.
Thanks in advance for your time!

CodePudding user response:

use dict

from collections import defaultdict
d = defaultdict(int)
with open(file='file.txt') as f:
    for line in f:
        k,v = line.spilt(',,')
        d[k]  = int(v)
for k,v in sorted(d.items(), key=lambda x:x[-1], reverse=True):
    print(f'{k},,{v}')
  • Related