vendredi 20 novembre 2015

how do I use a line from another file in a if statement on python3

I'm codeing on pyhton3 and I try to se if a line in a textfile is what i want, and if it is, then execute a code. However my "if" statement always returns "False", even if I know it to be "True".

My code looks like this:

x=open('myfile.txt','r');
r=x.readlines()

if r[2]=="seven":
    print("this works as I want")
else:
    print("noup! didn't work.")

The contents in myfile.txt is:

banana
car
seven
easter

The "if" statement returns "False" but I can't see why? If I "print (r[2])" it prints "seven" and if I replase the "if" statement to be smething else than a line from myfile.txt i can get it to be "True". So I hawe concluded that the problem is that i can't use a line from another file in a "if" statement. How to solve this problem?

Aucun commentaire:

Enregistrer un commentaire