Replace text with value form dictionary (regexp)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Pierre Bergamin

    Replace text with value form dictionary (regexp)

    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");
    [color=blue][color=green][color=darkred]
    >>> d = {}
    >>> d["x"] = 10
    >>> p = re.compile('{ ( [^}]* ) }', re.VERBOSE)
    >>> p.sub(r'd["\1"]','foo {x} bar')[/color][/color][/color]
    '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


  • Mirko Zeibig

    #2
    Re: Replace text with value form dictionary (regexp)

    Jean-Pierre Bergamin said the following on 03/03/2004 03:09 PM:[color=blue]
    > 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");[/color]

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

    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
    --

    Comment

    • Jean-Pierre Bergamin

      #3
      Re: Replace text with value form dictionary (regexp)

      Jean-Pierre Bergamin wrote:
      [color=blue]
      > 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");
      >[color=green][color=darkred]
      >>>> d = {}
      >>>> d["x"] = 10
      >>>> p = re.compile('{ ( [^}]* ) }', re.VERBOSE)
      >>>> p.sub(r'd["\1"]','foo {x} bar')[/color][/color]
      > 'foo d["x"] bar'
      >
      > How can I now put the actual value of d["x"] into the string 'foo
      > d["x"] bar'?[/color]

      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


      Comment

      • Scott David Daniels

        #4
        Re: Replace text with value form dictionary (regexp)

        Jean-Pierre Bergamin wrote:[color=blue]
        >I want to implement a small templating system where values in angle
        >brackets ...[/color]

        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.
        [color=blue]
        > I have this solution now:
        > ... d = {}
        > d["x"] = 10
        > d["y"] = "20"
        >[/color]
        def repl(template, replace):
        parts = template.split( '"')
        if len(parts) & 1 == 0: # Expect odd rails if even posts
        raise ValueError('Nee d 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)

        --
        -Scott David Daniels
        Scott.Daniels@A cm.Org

        Comment

        Working...