How can I determine the existence of a substring in a string using Python? The string is an html coded text string.
Determine existence of a substring in a string
Collapse
X
-
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."
Comment
-
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]Comment
-
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
Comment