String comparison doesn't seem to work for lines read from a file

前端 未结 2 1165
北恋
北恋 2020-11-27 08:39

I have a file named test.txt that contains the following:

Line 1
Line 2
Line 3
Line 4

I have simple Python program like this:<

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 09:02

    Strip the newline character:

    for line in f:
        print myline + " - " + line
        if myline == line.strip():
            print "We have a match!"
    

提交回复
热议问题