0

Hi

Having trouble getting this started. I wan't to read lines of data from my file, which has the current contents

Scores.txt -

Names             Difficulty                    Scores                Totals
Mark        Smith           2.5         6.0  5.0  5.5  5.5  6.5  6.0  6.5    44.25
John        Andrews         2.8         5.5  5.5  6.0  4.5  6.0  5.5  5.0    46.20

and using this code, read the file to extract certain fields for calculation

def main(): 
    fileIn = open( "scores.txt", "r") 
    lineIn = fileIn.readline() 
    while len(lineIn) != 0: 
        print lineIn 
        lineIn = fileIn.readline() 

main()

What would be a simple way to extract a field like 6.0 from scores to be used in my final calculation?

Should I define each field like 'scores' ' names' ' difficulty' in the code itself? or read from the whole file?

Any help would be appreciated

Edited by Ultralisk: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by cghtkh
0

You can try to split each line using

tokens = lineIn.split()
if len(tokens)==4:
   continue # skip the header
# For subsequent line
firstname = tokens[0]
lastname  = tokens[1]
...
totals    = float(tokens[-1])
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.