Python Forum
filtering and summing data in a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
filtering and summing data in a text file
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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])
Reply
#4
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().
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Summing up set elements(HH:MM:SS) tester_V 4 1,176 Dec-22-2022, 10:03 AM
Last Post: perfringo
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,132 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,690 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,015 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Summing up rows and columns plumberpy 3 2,280 Aug-18-2021, 05:46 AM
Last Post: naughtyCat
  Filtering a data frame according to number of occurences of an observation Menthix 1 1,887 May-31-2021, 10:50 PM
Last Post: supuflounder
Photo Filtering data and precision during calculations Scientifix 0 1,788 Mar-30-2021, 01:00 PM
Last Post: Scientifix
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,374 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  xml file creation from an XML file template and data from an excel file naji_python 1 2,124 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  Regex text file to store data in list TheSithSiggi 1 1,538 Dec-03-2020, 04:46 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020