[thelist] Python Regular Expressions (import re)

Tarrant Costelloe taz at madfolk.com
Fri Oct 24 09:25:24 CDT 2003


Fixed the problem. Solution was:

 > "The problem should not be just that it can't find that string, as A)
the string is there"
   Double checking that the string *actually is there*, grrr it must be
Friday.

-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Tarrant Costelloe
Sent: 24 October 2003 14:51
To: thelist at lists.evolt.org
Subject: [thelist] Python Regular Expressions (import re)


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.


-- 
* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester 
and archives of thelist go to: http://lists.evolt.org 
Workers of the Web, evolt ! 





More information about the thelist mailing list