Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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}')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading