Determine existence of a substring in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • loren41
    New Member
    • Mar 2010
    • 6

    Determine existence of a substring in a string

    How can I determine the existence of a substring in a string using Python? The string is an html coded text string.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Test for membership.
    Code:
    >>> 'sub' in 'Test for membership of a substring'
    True
    >>> 'nub' in 'Test for membership of a substring'
    False
    >>>

    Comment

    • loren41
      New Member
      • Mar 2010
      • 6

      #3
      I tried searching for the existence of the string, "Attorney of Record" in a large text string called, tarr_record. How can I test for True or False?

      Code:
      mystring = True
      
      # Search the retrieved html_string for "Attorney of Record"
      mystring = tarr_record
      "Attorney of Record" in mystring 
      if mystring:
          print "URL number ",url_record_number," has an attorney."
      else:
          print "URL number ",url_record_number," has NO attorney."
      Last edited by bvdet; Mar 18 '10, 03:56 PM. Reason: Add code tags

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Code:
        >>> mystring = "abcdefg   Attorney of Record hijklmnop"
        >>> "Attorney of Record".lower() in mystring.lower()
        True
        >>> if "Attorney of Record".lower() in mystring.lower():
        ... 	print "It's there"
        ... 	
        It's there
        >>>

        Comment

        • loren41
          New Member
          • Mar 2010
          • 6

          #5
          String Search

          Still having trouble verifying the presence of a substring. My string is called tarr_record and contains a complete web page in page source format. In all the search examples I've seen, the contents of the string are placed in quotes. My search is being performed on tarr_record, not a literal contained within quotes. How do I address this problem? Thanks for your continuing help.
          [CODE=python]
          # Program to read HTML file containing list of TARR files
          #
          import urllib
          import urllib2
          import linecache
          #
          #
          #Open a file that contains a list of 100 url addreses
          open(url_addres s_list, 'ru')
          #
          #Read a url address
          string = urllib.urlopen( url_address_lis t)
          # Read from the object, storing the page's contents in tarr_record.
          tarr_record = string.read()
          # tarr_record contains the source code for a web page
          string.close()
          # Test the source code for the text string "Attorney of Record"
          substring = tarr_record
          "Attorney of Record" in tarr_record
          if substring:
          print "URL number ",url_record_nu mber," has an attorney."
          else:
          print "URL number ",url_record_nu mber," has NO attorney."[/CODE]
          Last edited by bvdet; Mar 18 '10, 07:44 PM. Reason: Add code tags

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Please use code tags when posting code. See reply guidelines here.

            Try this:
            Code:
            # Test the source code for the text string "Attorney of Record"
            if "Attorney of Record" in tarr_record:
                print "URL number ",url_record_number," has an attorney."
            else:
                print "URL number ",url_record_number," has NO attorney."

            Comment

            • loren41
              New Member
              • Mar 2010
              • 6

              #7
              OS is Ubuntu Linux 9.10/Python Version is 2.6.4/Gui Tool is Dr. Python

              Comment

              • loren41
                New Member
                • Mar 2010
                • 6

                #8
                Thanks bvdet. As usual, I made the test much harder than it was. Ignorance is frustrating!

                Loren

                Comment

                Working...