0

I'm reading data from a file. Most of the data in the data file will be read in and written out to an output file without modification. However, in places the data is separated by a blank line, which precedes 5 other useless lines. Here is my code:

#Now loop over the entire file
while True:
testline = infile.readline()
if len(testline) ==0:
    break # EOF
elseif testline == '\n':
    for i in range(0,5)
        testline = infile.readline()
else:
    outfile.write(testline)

I get the following error that has me stumped:

File "convert.py", line 31
elseif testline == '\n':
              ^
SyntaxError: invalid syntax

Any thoughts? I can't figure out what is wrong with my elseif statement.

1
  • 3
    There is no elseif in Python. You want elif. Commented Jan 12, 2012 at 0:17

4 Answers 4

9

It isn't elseif in python, it's elif.

Sign up to request clarification or add additional context in comments.

Comments

5

Others have pointed out it's elif. Also, you're missing a colon at the end of your for statement, and Python will probably complain about the following line. So now you don't have to come back and ask a separate question for that. :-)

If you just want to iterate over the lines in a file, though, a better way to do this is:

for testline in testfile:
   # do stuff with the line

You don't have to check for the end-of-file that way because the loop automatically ends when you reach the end of the file.

1 Comment

While the rest of the answers provided answer your question explicitly, kindall here is right, you should really follow his approach.
2

It isn't 'elseif' it is 'elif' that you are looking for.

Furthermore there is another typo at the end of your for loop for i in range(0,5) it needs a : at the end there.

Comments

1

You could always try changing elseif to elif

And your indentation is stuffed from the 2nd line onwards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.