I have a text file as below.
l[0]l[1]l[2]l[3]l[4]l[5]l[6]
-----------------------------------
1| abc is a book and cba too
2| xyz is a pencil and zyx too
3| de
When using for l in fr
python doesn't return an array but a string for each line, that you have to process in the loop. Using l.strip().split()
will give you an array of strings, where a string would equal a word.
Then, is
is meant to compare object type, like is this line a string ? or an int ?
. So you cannot use it here. Use ==
to compare two objects of the same type.
Edit: some example code
import sys
fr = open("example.txt",'r')
for l in fr:
word = l.strip().split()
if word[3] == "book" or word[3] == "pencil":
# Do something
elif word[3] == "pen":
# Do something
fr.close()