-2

This is the text file I have:

#EMP_NO, EMP_NAME, AGE, POSITION, SALARY, YRS_EMP   
001, Peter Smyth, 26, Developer, 29000, 4
002, Samuel Jones, 23, Developer, 24000, 1
003, Laura Stewart, 41, DevOps, 42000, 15
004, Paul Jones, 24, Analyst, 21000, 2
005, Simon Brown, 52, Developer, 53000, 18
006, George Staples, 42, Tester, 42000, 12
007, Greg Throne, 57, DevOps, 50000, 23
008, Aston Bently, 27, Tester, 33000, 5
009, Ben Evans, 32, DevOps, 38000, 2
010, Emma Samson, 23, DevOps, 22000, 1
011, Stephanie Beggs, 43, Tester, 19000, 9
012, Sarah McQuillin, 47, DevOps, 23000, 5
013, Grace Corrigan, 48, Analyst, 44000, 16
014, Simone Mills, 32, DevOps, 32000, 11
015, Martin Montgomery, 28, Analyst, 28000, 3

I need to get all the salaries and add them together but I can only get the whole line returned, any ideas how to do this in python 3?

I have tried to get an answer from the following question, but this wasn't able to make an impact on my code: Tried Question

RC07JNR
  • 535
  • 1
  • 8
  • 24
  • 2
    What have you tried so far? – Klaus D. Nov 13 '18 at 16:15
  • Possible duplicate of [Read specific columns from a csv file with csv module?](https://stackoverflow.com/questions/16503560/read-specific-columns-from-a-csv-file-with-csv-module) – colidyre Nov 13 '18 at 16:19

1 Answers1

2
f = open('nameoffile.txt') #open file
sum = 0 #initialise
i = 0
for line in f: #read each line from file
    line = line.split(',') #read line as string, split to list of strings at the commas
    if(i>0): #to disregrad the first line that is the header
        sum += float(line[-2]) #salary is second from last, convert string to float to add
    i += 1   

print("Sum = ", sum)