I can’t seem to compare strings properly in python, what is the correct way to compare it in the if condition?
Testfile = "test.txt"
with open(TestFile) as testSet:
line = testSet.read().rstrip('\n')
if "#*#*# ham" or "#*#*# spam" in line:
print line
My test.txt something like looks like this:
#*#*# ham
foo bar bar foo bar bar bar bar
#*#*# ham
foo bar bar foo bar foo bar foo foo bar bar
#*#*# spam
foo bar foo foo bar barfoo bar foo foo bar bar
#*#*# spam
foo bar foo foo bar bar foo foo bar bar
#*#*# ham
foo bar foo foo
#*#*# spam
foo bar foo foo bar bar foo foo bar bar
#*#*# spam
bar foo bar foo foo bar
#*#*# spam
bar bar foo foo
Do:
instead of what you are doing. You are reading the whole file into the line variable, the way your code is.