Python Forum
filtering and summing data in a text file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: filtering and summing data in a text file (/thread-16875.html)



filtering and summing data in a text file - apexman - Mar-18-2019

i have a text file with columns of data, the first column has names that repeats and am trying to filter out the duplicates.
The other columns have numbers and would like to sum those numbers in the columns.
example of text file:

jackie 900 70
john 100 700
max 900 400
jen 800 23
john 700 400
jackie 600 300


RE: filtering and summing data in a text file - ichabod801 - Mar-18-2019

What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.


RE: filtering and summing data in a text file - apexman - Mar-18-2019

I've been working with this piece of code which i can use to output the various columns but i am unable to
successfully filter it.

for line in open("data.txt"):
    columns = line.split()
    if len(columns) >= 13:
        print (columns[0])



RE: filtering and summing data in a text file - ichabod801 - Mar-18-2019

A good way to do the filtering would be to have a set (used_names = set()). For each line, check the name. If it is in the set, it's a duplicate, and you can skip the rest of the loop with a continue statement. If it's not in the set, you put it in the set, and continue processing that row of the data. You can use a list for this instead of a set. It's just that a set is designed for quick contents checking.

Totals can be efficiently done while you are reading the data. Initialize them to zero before the loop, and add to them as you process the rows. You will need to convert the values to numbers using int() or float().