Hello,
I seem to be having some difficulties with Regular Expressions in
Python. I have the following "CreateHostingPlan" function:
#!/usr/local/bin/python
import re
import cgi
print "Content-Type: text/html\n\n"
# specify the filename of the template file
TemplateFile = "python_templates/hosting_template.html"
def CreateHostingPlan(Content):
TemplateHandle = open(TemplateFile, "r") # open in read only mode
# read the entire file as a string
TemplateInput = TemplateHandle.read()
TemplateHandle.close() # close the file
# this defines an exception string in case our
# template file is messed up
BadTemplateException = "There was a problem with the HTML
template."
SubResult = re.subn("<!-- *** INSERT HOSTING PLAN HERE *** -->",
Content,TemplateInput)
if SubResult[1] == 0:
raise BadTemplateException
print "Content-Type: text/html\n\n"
print SubResult[0]
In an attempt find out where the code is breaking I used 'print "test"'
statements on each line and found it to be:
SubResult = re.subn("<!-- *** INSERT HOSTING PLAN HERE *** -->",
Content,TemplateInput)
I know that it's finding the template file okay and as the line it's
breaking on is the first to use an instance of the "re" object I am
guessing something is up with that. The function itself is searching for
"<!-- *** INSERT HOSTING PLAN HERE *** -->" and replacing it with the
passed in "content" variable. The problem should not be just that it
can't find that string, as A) the string is there and B) the SubResult
checking code afterwards would handle this.
And ideas?
Regards,
Tarrant Costelloe
Technical Director
madfolk.com (.co.uk) - Reforming Media
(+44) 01903 523414.