Python - 2nd 'for' loop in function not working
Question: I may be missing the obvious here, and if so please excuse the noob (which I
am) question. I am trying to iterate through the lines in a text file. The
first `for` works fine; however, the 2nd `for` does not. The
`print(eachLine)`command does not print anything, and the result of the `newY`
list is a blank list (as it has been initialized). All the debugging I have
done (below is just the latest) points out to the 2nd `for` not being accessed
at all. What am I missing here?

Edit: There is a similar question
[here](http://stackoverflow.com/questions/14737980/two-for-loops-second-only-
executes-on-first-iteration-python), but in that one there are _nested_ loops
and the mistake is that the user was referring to the _same_ line in the _same
file_ in _nested_ loops. I am trying to loop through the file all over again,
from scratch.

    
    
    import os
    os.chdir ('d:\Documente\python tests')
    
    def plotRegression(myFile):
        lineCounter=0
        sumX=0
        sumY=0
        sumXY=0
        sumX2=0
        newY=[]
    
        for eachLine in myFile:
            coords=eachLine.split()
            lineCounter=lineCounter+1
            sumX=sumX+float(coords[0])
            sumY=sumY+float(coords[1])
            sumXY=sumXY+float(coords[0])*float(coords[1])
            sumX2=sumX2+float(coords[0])**2
    
        avgX=sumX/lineCounter
        avgY=sumY/lineCounter
        m =(sumXY-lineCounter*avgX*avgY)/(sumX2-lineCounter*avgX**2)
    
        for eachLine in myFile:
            print (eachLine)
            coords=eachLine.split()
            newY.append(avgY+m*(coords[0]-avgX))
    
        return (avgX, avgY,sumXY, sumX2, m, newY)
    
    
    def Main():
        dataFile = open("labdata.txt","r")
        print (plotRegression(dataFile))
        dataFile.close()
    
    
    Main()
    


Answer: You'll need to _rewind_ your file read position to the start, using
[`file.seek()`](https://docs.python.org/2/library/stdtypes.html#file.seek):

    
    
    myFile.seek(0)
    

Files are streams; reading from a file or writing to it advances a file
pointer to a new position. Iterating over a file to read from it line by line
is no exception. Once you've read all the lines in a file, the file position
is left all the way at the end and iterating again won't produce any
additional information.

It'd be a better idea to process all the information from the file and store
it in memory, reading just _once_. File reading is a slow process, compared to
accessing the same information in memory. In this case, you only need to use
the first value from each line; store that when reading in the first loop:

    
    
    x_coords = []
    for eachLine in myFile:
        x, y = (float(c) for c in eachLine.split())
        lineCounter += 1
        x_coords.append(x)
        sumX += x
        sumY += y
        sumXY += x * y
        sumX2 += x ** 2
    
    avgX = sumX / lineCounter
    avgY = sumY / lineCounter
    m = (sumXY - lineCounter * avgX * avgY) / (sumX2 - lineCounter * avgX ** 2)
    
    for x in x_coords:
        newY.append(avgY + m * (x - avgX))
    

