0

Im searching for a string in a file, but even though there is matching string in the file it always returns false. Where am I going wrong?

file = open('temp.txt', 'r')

def search(userinput, file):

    file.seek(0)
    filecontent = file.readlines()
    for i in filecontent:
        sp = i.split(' ')
        t_name = sp[0] + ' ' + sp[1]
        print (t_name)
    if (t_name == userinput):
        return True
    else:
        return False


searchstr = 'Peter Piper'
found = search(searchstr, file)
print (found)
file.close

temp.txt

Peter Piper 20 30
Tom Cat 10 20
Jerry Mouse 30 50
Harwee
  • 1,601
  • 2
  • 21
  • 35
Varun
  • 117
  • 1
  • 4
  • 14
  • The suggested question and solution seems to be quite different from mine. Im having an indentation problem in if condition, i case I include it under for loop its checking only the first line, in case I don't include it under for loop as shown above it seems to be returning false always. – Varun Jul 13 '17 at 17:32

3 Answers3

1

To keep in sync with the code example that you used to explain what specifically is going wrong...

Your problem is that you are only actually checking whether t_name is userinput AFTER the for loop runs. What you want to do is this

Peter Piper 20 30
Tom Cat 10 20
Jerry Mouse 30 50




file = open('temp.txt', 'r')

def search(userinput, file):

    file.seek(0)
    filecontent = file.readlines()
    for i in filecontent:
        sp = i.split(' ')
        t_name = sp[0] + ' ' + sp[1]
        print (t_name)
        if (t_name == userinput):
            return True
    return False


searchstr = 'Peter Piper'
found = search(searchstr, file)
print (found)
file.close

In my code example, each time the for loop runs, it checks whether the names match, and if they do it ends the function and returns True. If it never ends the function and returns True before the end of the for loop, that means none of the names were a match, and it should return False after the for loop completes

Mark R
  • 337
  • 2
  • 9
  • It worked. Thanks a lot.. I knew it was an indentation problem but couldn't quite figure out a solution. Thanks again :) @Mark R – Varun Jul 13 '17 at 17:40
0

You can try this:

f = open('filename.txt').readlines()

f = [i.strip('\n') for i in f]

word = 'Peter Piper'
if any(word in i for i in f):
    print "word exists in file"
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • Why not just iterate directly over the file: `for line in open('filename.txt')`, and then compare the `word` to each `line`: `if word in line:`? Also, it's best practice to use the context manger when opening files as it ensures your file will always be closed. – Christian Dean Jul 13 '17 at 17:30
0

the problem is you are checking outside loop where t_name is Jerry Mouse which is last value, it should be like

file = open('temp.txt', 'r')

def search(userinput, file):
    file.seek(0)
    filecontent = file.readlines()
    for i in filecontent:
        sp = i.split(' ')
        #print(sp)
        t_name = sp[0] + ' ' + sp[1]
        print (t_name)
        if (t_name == userinput):
            return True
    return False


searchstr = 'Peter Piper'
found = search(searchstr, file)
print (found)
file.close
uingtea
  • 6,002
  • 2
  • 26
  • 40