Here is my code, what I'm having problems with is taking values from a 10 field file (scores.dat) and putting them into a list. How would i go about this?

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

main()

Recommended Answers

All 9 Replies

Can you post score.dat or a sample of the file.
Show what data from the file you will take out,and how the list should look.

Here is my code, what I'm having problems with is taking values from a 10 field file (scores.dat) and putting them into a list. How would i go about this?

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

main()

This is how the values appear in the file each field divided by ','

So the first 2 fields for each line are First name, last name respectively

John, Smith, 2.5, 6.0, 5.0, 5.5, 5.5, 6.5, 6.0, 6.5 
Tony, Edwards  , 2.8, 5.5, 5.5, 6.0, 4.5, 6.0, 5.5, 5.0
with open("scores.dat") as fileIn:
    for line in fileIn:
        if line:
            parse(line)

(Something like that, I'm not sure if I wrote that correctly. I haven't been doing much Python lately.)

How would I use the while loop to read the next line within the file?

fileIn.next().split(',') or fileIn.readline().split(',')

Thanks Tony, but I'm getting a strange issue

my output is looking like this

>>>
John Smith 2.5 6.0 5.0 5.5 5.5 6.5 6.0 6.5

>>>

my code

def main(): 
    fileIn = open( "dive_details.dat", "r") 
    lineIn = fileIn.readline()
    
    

    while len(lineIn) != 0:
        print lineIn
        
        #lineIn = fileIn.readline() 
        lineIn = fileIn.readline().split()

It seems to skip the first line in the file, but it adds the each line after properly. Not to mention the space between the first and second lines.

this is how they appear in the file

John Smith 2.5 6.0 5.0 5.5 5.5 6.5 6.0 6.5
Tony Edwards 2.8 5.5 5.5 6.0 4.5 6.0 5.5 5.0
Joey Cusack 2.6 4.0 4.5 5.5 5.0 5.0 6.5 5.0

Any ideas?

How about:

def main(): 
    fileIn = open( "dive_details.dat", "r") 
    lineIn = fileIn.readline().split()

    while lineIn:
        print lineIn
        lineIn = fileIn.readline().split()
    fileIn.close()  #please remember this

It skips the first line because you aren't spliting it. Try this (which I very slightly modified from @jcao219's post)

with open("scores.dat") as fileIn:
# with statements are cool: http://effbot.org/zone/python-with-statement.htm
    for line in fileIn: # this loops over every line in the file
        if line: # this line skips empty lines in the file
            the_list = [x.strip() for x in line.split(',')]
            # create a list by splitting on comma then stripping off any white space
            print(the_list) # prove it worked

You've got newline in line and the print statement has added one more. The first line you do not split, but overwrite at line 11.
All you need to do is:

with open( 'scores.dat') as filein:
    print ([line.split(',') for line in filein]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.