I've used a very similar loop in the past and had no issues. Here is the code:

    # Read the file
    file = open("db.csv", "r")
    # Get a line from the file
    line = file.readline()
    # Stop if we did not get a line
    if not line: break
    # Remove junk characters
    line = line.replace("\n", "")
    line = line.replace("\r", "")
    # Divide into parts
    parts = line.split(",")
    investor = parts[0]
    acct_num = parts[1]
    acct_type = parts[2]
    amount = parts[3]
    # Output the data from the csv file into table rows
    # Close the file

So, the issue I'm having is that when I run it, the while loop iterates over the first line indefinitely (requiring a manual stop). Every other time I constructed a loop like this the while loop iterates over each successive line in the file rather than the same line over and over again and the "if not line: break" statement has prevented unchecked recursion. What am I doing wrong? I tried converting it into a for loop as a workaround, but that just created more problems.


Every time the loop runs, you are opening the file so you are starting at the beginning of the file. You open the file, read the first line in, parse it, print it out, close the file, loop around, open the file, read the first line in, etc etc etc

Your open/close shouldn't in the loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .