R Rip
R Rip

Reputation: 19

Syntax error for else and elif

I am trying to separate a file of 1500 lines into metadata and data.

Here is what I have:

headerLines = []
dataLines = []
for line in lineList:
    if (len(line) > 0 and (line[0] == # )) :
        headerLines.append(line)     
    elif (len(line) > 0 and (line[0] == U ):
        dataLines.append(line)
print("we have {} lines of metadata".format(len(headerLines)))
print("we have {} lines of data".format(len(dataLines)))       

#here we want to seperate the lines in the file into headerLines and dataLines

Upvotes: 1

Views: 427

Answers (2)

Eric Chao
Eric Chao

Reputation: 41

This line need ')' at the end. Like this: elif (len(line) > 0 and (line[0] == U )):

Another problem: if (len(line) > 0 and (line[0] == '#' )):

'#' is an operator.Just like +/-, you cannot compare 'aaa' to +/-.

Upvotes: 0

Eric Swedlo
Eric Swedlo

Reputation: 46

Your problem is in your parsing. Change the line:

    if (len(line) > 0 and (line[0] == # )) :

to

    if (len(line) > 0 and (line[0] == '#' )):

What is happening is that the hash (#) is seen as a comment, and as such everything after it is ignored (that's why it's grey). What I did to fix it is I changed it into a string, which inevitably fixes another problem with the script anyways. If line[0] is a piece of text with the value #, then it will be printed as a string, '#', so if we check for that instead, it will work just fine.

You are doing an amazing job for someone with dyslexia, and good on you for trying to learn.

Upvotes: 1

Related Questions