Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: How do you search for a string in Python and then return True or False?

  1. #1
    Join Date
    Dec 2007
    Beans
    35
    Distro
    Ubuntu 10.04 Lucid Lynx

    How do you search for a string in Python and then return True or False?

    Hi, this should be a fairly straightforward problem but I'm still getting to grips with programming.

    What I wish to do is to search for a string in an xml file using python and then for it to say True or False, depending on whether the string appears within the file or not.

    At the moment I've got this:

    >>> datafile = file('data.xml')
    >>> for line in datafile:
    ... if '168439992' in line:
    ... print 'True'
    ... else:
    ... print 'False'
    ...
    False
    False
    True
    False

    So at the moment it goes through each line in the xml file and says whether the string appears or not, whereas I would like it only to say True or False just once.

    Thanks in advance

  2. #2
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How do you search for a string in Python and then return True or False?

    You shouldn't print at each line but only at the end, when you have analyzed the whole file or as far as needed:

    Code:
    found = False
    for line in datafile:
        if '168439992' in line:
            found = True
            break
    print found

  3. #3
    Join Date
    Dec 2007
    Beans
    35
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How do you search for a string in Python and then return True or False?

    Thank you for the quick reply, but at the moment it shows up False although 168439992 does appear in the file. Is this because I'm in interactive mode? And could you please clarify what break means? Does that mean not checking the next line?

  4. #4
    Join Date
    Oct 2008
    Beans
    42

    Re: How do you search for a string in Python and then return True or False?

    What about:

    Code:
    datafile = file('data.xml')
    '168439992' in datafile
    Way more pythonic IMHO.

  5. #5
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How do you search for a string in Python and then return True or False?

    Interactive mode shouldn't matter, it's the same file and Python works the same. Could you post what you did in interactive mode? Have you correctly indented the for-loop so that it really loops over all the lines of the file?

    break means the for-loop is exited and it therefore no longer does all the remaining lines. That makes sense because when you've found the string in the line, you no longer need to search the remaining lines.. you already have all the information you need to give the right answer.

  6. #6
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How do you search for a string in Python and then return True or False?

    I just checked: you have to reload the file in interactive mode each time you want to test it out. So do the datafile = file('..') line again and then the for-loop code. Or better yet, make it a function, if you're up to that:

    Code:
    def check():
        datafile = file('data.xml')
        found = False
        for line in datafile:
            if '168439992' in line:
                found = True
                break
        print found
    And then run
    Code:
    check()
    to test.

    Quote Originally Posted by javierrivera View Post
    What about:

    Code:
    datafile = file('data.xml')
    '168439992' in datafile
    Way more pythonic IMHO.

    That does not appear to work.

  7. #7
    Join Date
    Dec 2007
    Beans
    35
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How do you search for a string in Python and then return True or False?

    Quote Originally Posted by javierrivera View Post
    What about:

    Code:
    datafile = file('data.xml')
    '168439992' in datafile
    Way more pythonic IMHO.
    I know, but somehow it doesn't work, I don't know why.

    I just tried your code (simeon87) without the found='False' to begin with and then it works. I think that once it exits the for loop, the found variable changes back to False for some reason, but thank you for the help, I will just leave out the found='False' to begin with and just check whether the variable is empty or 'True'. It's not perfect but definitely good enough for what I'm trying to do.

    Thanks again for your help

  8. #8
    Join Date
    Dec 2007
    Beans
    35
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How do you search for a string in Python and then return True or False?

    Quote Originally Posted by simeon87 View Post
    I just checked: you have to reload the file in interactive mode each time you want to test it out. So do the datafile = file('..') line again and then the for-loop code. Or better yet, make it a function, if you're up to that:

    Code:
    def check():
        datafile = file('data.xml')
        found = False
        for line in datafile:
            if '168439992' in line:
                found = True
                break
        print found
    And then run
    Code:
    check()
    to test.




    That does not appear to work.
    Thank you, that's perfect

  9. #9
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How do you search for a string in Python and then return True or False?

    Quote Originally Posted by Christian Christiansen View Post
    I just tried your code (simeon87) without the found='False' to begin with and then it works. I think that once it exits the for loop, the found variable changes back to False for some reason, but thank you for the help, I will just leave out the found='False' to begin with and just check whether the variable is empty or 'True'. It's not perfect but definitely good enough for what I'm trying to do.
    I'm not sure what you're doing differently that makes it do that. The idea of setting it to False initially is that the variable always has a value so there's no empty variable case. The found variable is False, you walk over all the lines of the file and only when the string is found, it is set to True. Afterwards, this guarantees that the variable found has either the value False or True but not empty, which is what you want.

  10. #10
    Join Date
    Dec 2007
    Beans
    35
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How do you search for a string in Python and then return True or False?

    Quote Originally Posted by simeon87 View Post
    I'm not sure what you're doing differently that makes it do that. The idea of setting it to False initially is that the variable always has a value so there's no empty variable case. The found variable is False, you walk over all the lines of the file and only when the string is found, it is set to True. Afterwards, this guarantees that the variable found has either the value False or True but not empty, which is what you want.
    I wrote that before I had seen your new post, your new post has cleared the problem. Basically what I had done was give found a string instead of a value, so found = 'True' instead of found = True. That was what I meant.

    Anyways, I'm going to try and see whether this works within my program but I hope I'll be able to solve everything from now on.

    Thank you once again

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •