String substitution -- reactions?

sdd daniels at dsl-only.net
Mon Nov 3 15:20:33 EST 2003


Here's a sample of how to do string substitution for a string with
double quotes around the names to be substituted.  The following
is 2.3 code, but 2.2 code is also listed in the recipe at:

   http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/231347

   def dequote(text, dictionary):
       """Replace quoted parts of string with dictionary entries."""
       parts = text.split('"')
       parts[1::2] = [dictionary[word] for word in parts[1::2]]
       return ''.join(parts)

So, dequote('''Dear "user", we here at "provider" want to ....''',
		dict(user='Resident', provider='Att.Net'))
can be used to fill out forms (or prepare html pages).  If you need
to be able to include double quotes, simply make sure that the
dictionary provided get an entry like the one in {'':'"'}, then
using doubled double quotes may be used to represent quote marks,
as is common in a number of languages.

-Scott David Daniels
Scott.Daniels at Acm.Org





More information about the Python-list mailing list