dimanche 17 janvier 2021

I read file by python, But I can't use correctly "if"

I write python code on Raspberry Pi(CM3+). I would like to get data from the file and operate "if". My code is the following. I read the file, "sample.txt". It is "center, wave22, OK". "print(flg0), print(flg1), print(flg2)" display "center, wave22, OK, NNN". It means "readlines" read correctly the file. But, "if flg0 == "center"" doesn't operate correctly. In spite of "flg0" equal "center" , it displays "N", not "Y". What should I do?

f = open("sample.txt")
contents = f.readlines()
flg0 = contents[0]
flg1 = contents[1]
flg2 = contents[2]

print(flg0)
print(flg1)
print(flg2)

if flg0 == "center":
    print("Y")
else:
    print("N")

if flg1 == "wave22":
    print("Y")
else:
    print("N")
if flg2 == "OK":
   print("Y")
else:
   print("N")

Aucun commentaire:

Enregistrer un commentaire