How to return multiple #s in same line after split()

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

    #1

    How to return multiple #s in same line after split()

    This is what I would like it to do.
    Enter the search file to look in April_hours
    Enter your search item 50 150

    #example of text in document
    April1, 50 100 150 75 200
    April2, 100 150 200 75 250
    April3, 50 75 100 150 200

    2 of your search was found

    I have been working on it and it is working partially. Although I can not get it to count them as a group in the same line yet. I am trying to go line by line now to check for the group with a while loop, so if all are in the line it will add to the count and if any one of them are not then it will go to the next line. I haven't decided if this is the best way I can do it or not.
    The code dosn't have the readline() and loop because I have not been able to make it work.

    If anyone can help please let me know if I am going about this the correct way.
    Code:
    look_in = raw_input ("Enter the search file to look in ")
    search_terms = raw_input ("Enter your search item ").split() # Note the final ()
    
    data = open(look_in).read()
    count = 0
    for term in search_terms:
        count += data.count(term)
    if count:
        print count
    else:   print "Your search was not found"
    Last edited by bvdet; Feb 12 '10, 02:34 AM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please use code tags when posting code. See posting guidelines here.

    Check out the following code. Something similar should work for you.
    Code:
    s = '''April1, 50 100 150 75 200
    April2, 100 150 200 75 250
    April3, 50 75 100 150 200
    '''
    
    sList = s.split("\n")
    
    searchTerm = "200, 75, 50 "
    searchTerms = [item.strip() for item in searchTerm.split(',')]
    
    for item in sList:
        itemList = item.split()
        if False not in [term in itemList for term in searchTerms]:
            print "Search terms were found in line item '%s'" % (item)
    Output:
    Code:
    >>> Search terms were found in line item 'April1, 50 100 150 75 200'
    Search terms were found in line item 'April3, 50 75 100 150 200'
    >>>
    BV - Moderator

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      I would consider doing this with regular expressions. Very powerful once you get the knack of them. Bit of a fiddle in this case, but check out the following:

      Code:
      import re
      
      look_in = raw_input ("Enter the search file to look in ")
      search_terms = raw_input ("Enter your search item ").split(',')
      
      search_pattern=r""
      for st in search_terms:
          search_pattern+=".* "+st.strip()
      search_pattern+=" .*"
      #search_pattern is off the format ".* 50 .* 150 .*"
      
      file = open(look_in)
      count = 0
      for line in file:
          if re.match(search_pattern,line+" "):
              count+=1
      if count:
          print count,"items found"
      else:
          print "No items found"
      Let me know if it doesn't work. I haven't had a chance to try it myself!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Good idea Glenton to use regular expressions. Modifying my previous simplified example for re:
        Code:
        import re
        
        s = '''April1, 50 100 150 75 200
        April2, 100 150 200 75 250
        April3, 50 75 100 150 200
        '''
        
        sList = [item for item in s.split("\n") if item]
        
        searchTerm = "250, 75, 150 "
        searchTerms = [item.strip() for item in searchTerm.split(',')]
        
        for item in sList:
            if None not in [re.search(r"\b%s\b" % (term), item) for term in searchTerms]:
                print "Search terms were found in line item '%s'" % (item)
        Output:
        Code:
        >>> Search terms were found in line item 'April2, 100 150 200 75 250'
        >>>
        Special character "\b" matches the word boundaries.

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          Hey, that's neat! Nice trick...

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Regular expressions are interesting to me because applying them can really make you think. Much of the time other solutions can be used and may be better, but in this case a simple re expression looks ideal.

            BV

            Comment

            Working...