2

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'])
user2819379
  • 73
  • 1
  • 4
  • Are you looking for the [`upper()`](http://docs.python.org/2/library/stdtypes.html#str.upper) method? `if content.upper() in my_list: print(content.upper())` will do a case-insensitive comparison and print the content in all uppercase. If yes then this question is a [duplicate](http://stackoverflow.com/questions/9257094/how-to-change-a-string-into-uppercase) – Bakuriu Feb 24 '14 at 13:34
  • 2
    For starters, `file.read()` returns a single string, so when you do `for line in data`, you're actually iterating through each individual letter in the file. – Kevin Feb 24 '14 at 13:37
  • @André I have edited the question kindly have a look – user2819379 Feb 24 '14 at 13:42
  • Just to be clear @Kevin is trying to tell you that data = file.read() returns a string which contains your entire file. If you want to look at individual lines then do a readlines.() instead of read(). You can get all words by read().upper.().split() – PyNEwbie Feb 24 '14 at 13:43
  • Thank you for your suggestions people, I shall post the solution once am able to – user2819379 Feb 24 '14 at 13:50

2 Answers2

2

I think you are looking for file.readlines(). file.read() will create a single string, so you are iterating through each character of that string. file.readlines() will create a list of strings, where each string is part of the file, separated by the occurrence of newlines. The docs help explain this.

2rs2ts
  • 10,662
  • 10
  • 51
  • 95
0
import re

MyList = ['WORD1', 'WORD2', 'WORD3']

file = open('/home/chi/Desktop/hello/grepoutput.txt','r') 
data = file.readlines() #THIS 
file.close()

for line in data:
    for content in line.split():
        if content in MyList:
            print content 
user2819379
  • 73
  • 1
  • 4