Thread: Simple Question

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Simple Question


    I'm looking for the simplest code to check if an imputed file contains an imputed string. If the the file does contain the sting, I would like the Python shell to print: 'Yes', and if not, to print: 'No'.

    Thanks
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Originally Posted by dannyz_au
    I'm looking for the simplest code to check if an imputed file contains an imputed string. If the the file does contain the sting, I would like the Python shell to print: 'Yes', and if not, to print: 'No'.

    Thanks
    Code:
    with open(<input file name>) as f:
        if <input string> in f.read(): print "yes"
        else: print "no"
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    print(('No','Yes')[goal in text_to_be_searched])

    You can choose how you populate the goal and the text_to_be_searched strings. source is a file name.
    Code:
    >>> with open(source,'r') as pf:
    ...     text_to_be_searched = pf.read()
    ... 
    >>> import sys
    >>> goal = sys.stdin.read()[:-1]
    mighty
    mouse
    ^D
    >>> print(('No','Yes')[goal in text_to_be_searched])
    No
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo