How do I get my search to return more then one word or number in a sentences

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chuckiechan
    New Member
    • Feb 2010
    • 2

    How do I get my search to return more then one word or number in a sentences

    I am a beginner so forgive me if my code looks like a joke.

    I can return a single character, but I don't know if I am using the wrong format I was trying the split method because I thought I could return several parts of a sentence for example if it had 1 2 3 4 5 6 7 I thought I could search and return
    2 5 7 but it will only work if they are next to each other or an exact match I guess. I may have don it incorrectly. I couldn't make the split work.

    This is the last somewhat working version I had.
    Code:
    look_in = raw_input ("Enter the search file to look in ")
    search = raw_input ("Enter your search item ")
    file = open(look_in, "r").read().count(search)
    if file:   print search, ",", file,"Of your search was found"
    else:   print "Your search was not found"
    If anyone could help or point me in the right direction I would appreciate it.
    Thanks for your time and help.
    Last edited by bvdet; Feb 2 '10, 03:09 AM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please use code tags when posting code.

    You are on the right track with split. You need to loop on the list that results from the split. Here's an example: I have a file full of 5 letter words. I will skip the raw_input.
    Code:
    fn = "fiveLetterWords.txt"
    # Do not use "file" for a variable name.
    # The built-in function "file()" will be masked.
    fileStr = open(fn).read().lower()
    words = 'Clunk,unite,fatal,PLIES,abstemious'
    for word in words.lower().split(','):
        i = fileStr.count(word)
        print ("There %s %d occurrence%s of %s in file %s" % \
               (["is", "are"][(i>1 or i==0) or 0],
                i, ["", "s"][(i>1 or i==0) or 0], word, fn))
    The output:
    Code:
    >>> There is 1 occurrence of clunk in file fiveLetterWords.txt
    There is 1 occurrence of unite in file fiveLetterWords.txt
    There is 1 occurrence of fatal in file fiveLetterWords.txt
    There is 1 occurrence of plies in file fiveLetterWords.txt
    There are 0 occurrences of abstemious in file fiveLetterWords.txt
    >>>
    Please ask if you have any questions about the code I posted.

    Comment

    Working...