I need to compare the words in my grepoutput.txt and MyList and print out those that are common but am getting individual alphabets as output without any comparison. Kindly help. Thank you.
MyList = ['WORD1', 'WORD2', 'WORD3']
file = open('/home/~/grepoutput.txt','r')
data = file.read()
file.close()
for line in data:
for content in line.split():
if content in MyList:
print content
The grepoutput.txt consists of : hello world
WORD1 WORD2 WORD3 WORD4
I also tried using set logic but in vain
setoutput = set(MyList) & set(content)
print setoutput
And here is the output:
[]
searching now...
W
set(['W'])
O
set(['O'])
R
set(['R'])
D
set(['D'])
1
set(['1'])
set(['\n'])
W
set(['W'])
O
set(['O'])
R
set(['R'])
D
set(['D'])
2
set(['2'])
set(['\n'])
W
set(['W'])
O
set(['O'])
R
set(['R'])
D
set(['D'])
3
set(['3'])
set(['\n'])
H
set(['H'])
e
set(['e'])
l
set(['l'])
l
set(['l'])
o
set(['o'])
set(['\n'])