Replace text with value form dictionary (regexp)

  • Thread starter Jean-Pierre Bergamin
  • Start date
J

Jean-Pierre Bergamin

Hi there

I want to implement a small templating system where values in angle brackets
should be replaced by the corresponding dicitionary value ("foo {x} bar" ->
"foo 10 bar");
d = {}
d["x"] = 10
p = re.compile('{ ( [^}]* ) }', re.VERBOSE)
p.sub(r'd["\1"]','foo {x} bar')
'foo d["x"] bar'

How can I now put the actual value of d["x"] into the string 'foo d["x"]
bar'?


Thanks for your help.


Regards

James
 
J

Jean-Pierre Bergamin

Jean-Pierre Bergamin said:
I want to implement a small templating system where values in angle
brackets should be replaced by the corresponding dicitionary value
("foo {x} bar" -> "foo 10 bar");
d = {}
d["x"] = 10
p = re.compile('{ ( [^}]* ) }', re.VERBOSE)
p.sub(r'd["\1"]','foo {x} bar')
'foo d["x"] bar'

How can I now put the actual value of d["x"] into the string 'foo
d["x"] bar'?

I have this solution now:
import re

d = {}
d["x"] = 10
d["y"] = "20"

def repl(matchobj):
return str(d[matchobj.group(2)])

p = re.compile('({ ([^}]*) })', re.VERBOSE)
print p.sub(repl, 'foo {x} bar {y} gugu')

Any better solutions?


Regards

James
 
M

Mirko Zeibig

Jean-Pierre Bergamin said the following on 03/03/2004 03:09 PM:
Hi there

I want to implement a small templating system where values in angle brackets
should be replaced by the corresponding dicitionary value ("foo {x} bar" ->
"foo 10 bar");

Hm, why don't you just use the "normal" pyformat-replacements ala
"%(KEY)FORMAT":

print """
<html>
<head>
<title>%(title)s</title>
</head>
<body>
<h1>%(title)s</h1>
I got %(amount)0.2f dollars
</body>
</html>
""" % { 'title': 'My account', 'amount': 3.5 }

Regards
Mirko
--
 
S

Scott David Daniels

Jean-Pierre Bergamin said:
I want to implement a small templating system where values in angle
brackets ...

If you can drop the angle brackets (which in this example even seem to
be braces) and switch to a single delimiter (such as '"' or '|'), you
could use some old magic that now works in 2.3 for normal vectors:
stride expressions.
I have this solution now:
... d = {}
d["x"] = 10
d["y"] = "20"
def repl(template, replace):
parts = template.split('"')
if len(parts) & 1 == 0: # Expect odd rails if even posts
raise ValueError('Need an even number of quotes, not %d.'
% template.count('"'))

parts[1::2] = [str(replace[word] for word in parts[1::2]]
return ''.join(parts)

repl('"x"+"y" is x+y.', d)

A string like 'This is x: "x' will accidentally replace the final x,
unless you leave the exception test in the code. If you add an
entry to your dictionary like: d[''] = '"', you can use doubled
quotes to indicate single quotes in the output.

d[''] = '"'
repl('"x"+"y" is ""x+y"".', d)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top