Allan
Allan

Reputation: 41

Searching word from file_1 in file_2

I need to have following output: Function should return True, if any word from file 1 is found in file 2. otherwise function should return False: In file_1 each line should consists of one word.

def search_in_file(filepath_1, filepath_2):
    wordlist_1=[]
    f = open(filepath_1, "r")
    for line in f:
        wordlist_1.append(line)
    print wordlist_1

    wordlist_2=[]
    f = open(filepath_2, "r")
    for line in f:
        wordlist_2.append(line)
    print wordlist_2

    for i in wordlist_1:
        if i in wordlist_2:
            return True
        else:
            return False

I still got False, but some of words from file_1 are visible in file_2. Could someone help?

Upvotes: 2

Views: 66

Answers (3)

Vivek Sable
Vivek Sable

Reputation: 10223

  1. use with statement, open, and read method to get file content.
  2. Use split() method to create list of file content.
  3. Use set method to get common values from two list

input:

File: "/home/vivek/Desktop/input1.txt"

file first 
I have some word from file second
Good
1 2 3 4 5 6 7

File: "/home/vivek/Desktop/input2.txt"

file second 
I have some word from file first
Good 
5 6 7 8  9 0

Code:

def searchInFile(filepath_1, filepath_2):
    with open(filepath_1, "r") as fp:
        wordlist_1 = fp.read().split()

    with open(filepath_2, "r") as fp:
        wordlist_2 = fp.read().split()

    common = set(wordlist_1).intersection(set(wordlist_2))

    return list(common)


filepath_1 = "/home/vivek/Desktop/input1.txt"
filepath_2 = "/home/vivek/Desktop/input2.txt"

result = searchInFile(filepath_1, filepath_2)
print "result:", result
if result:
    print "Words are common in two files."
else:
    print "No Word is common in two files."

Output:

infogrid@infogrid-172:~$ python  workspace/vtestproject/study/test.py
result: ['Good', 'word', 'file', 'I', 'have', 'some', 'second', '5', '7', '6', 'from', 'first']
Words are common in two files.

Upvotes: 0

jithinodattu
jithinodattu

Reputation: 90

def search_in_file(filepath_1, filepath_2):
 wordlist_1=[]
 f = open(filepath_1, "r")
 for line in f:
     wordlist_1.append(line)
 print wordlist_1

 wordlist_2=[]
 f = open(filepath_2, "r")
 for line in f:
     wordlist_2.append(line)
 print wordlist_2

 for i in wordlist_1:
     if i in wordlist_2:
         return True
 return False

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336188

You could use sets for this:

def search_in_file(filepath_1, filepath_2):
    wordlist_1=set(open(filepath_1))
    wordlist_2=set(open(filepath_2))
    return wordlist_1 & wordlist_2 != set() # Check if set intersection is not empty
    # Of course, you could simply return wordlist_1 & wordlist_2,
    # that way you'd know the actual set of all matching words.

Note that line endings are preserved when reading a file line by line. Therefore, if the last line of the file doesn't end in a newline, matches may be missed.

Upvotes: 2

Related Questions