Ajax
Ajax

Reputation: 1749

python parsing : String Comparison issue

for line in f.readlines():
    if( line == "Open Ended Schemes(Balanced)") :
        print "found"

My data text is Mutual fund information given by this link.

When I find the 'Open Ended Schemes', I wish to execute some code. The problem is that == operator doesn't work here. I tried some combinations with line.rsplit() but couldn't succeed.

I am a beginner with Python

Upvotes: 2

Views: 1601

Answers (1)

MartianInvader
MartianInvader

Reputation: 131

The readlines() method in python leaves the newline character at the end of each line. Just change it to

for line in f.readlines():
    if( line == "Open Ended Schemes(Balanced)\n") :
        print "found"

Alternately, you could strip the newline from your line string before checking:

for line in f.readlines():
    if( line[:-1] == "Open Ended Schemes(Balanced)") :
        print "found"

This should work as well.

Upvotes: 3

Related Questions