Python 3 - sum of all numbers in a file -


i have directory consisting of many files, each having of format :

vertie,f,5 wilma,f,17 john,m,11 william,m,15 

for files need check if particular name occurs @ specific line number. if sum of 3rd column 'f'.

i have written following code doesn't seem work :

        f1 = open('abc.txt')         f2 = open('abc.txt')         i, line in enumerate(f1):             line = line.strip()             name, sex, count = line.split(',')             if == 2 , name == 'wilma':                 line in f2:                     line = line.strip()                     name, sex, count = line.split(',')                     if sex == 'f':                         result += int(count.strip()) 

any tips going wrong?

any reason you're using python this? it's pretty simple in awk:

 awk -f, 'begin{sum=0}/wilma/{sum+=$3} end{print sum}' file1 file2 ... 

edit based on comment: safe, can explicitly check first field wilma:

 awk -f, '{if($1=="wilma"){sum+=$3}} end{print sum}' file1 file2 ... 

Popular posts from this blog