executea string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Groleo Marius

    #1

    executea string

    I have the folowing code:
    c=2
    e=3
    s="12"
    code.append('fu ntion ("c, e,s")')
    print "\n".join(c ode) + '\n'

    The problem is when i call append.
    The variables s, c, e are not replaced with their value, so in the end
    i get the result:

    funtion(" c, e, s");

    The result that i want is :
    funtion("2,3,12 ");



    --
    Regards,
    .''`. # touch universe
    : :' : # chmod +rwx universe
    `. `'` # ./universe
    `- Debian - when you have better things to do than fix a system
  • Arjen Dijkstra

    #2
    Re: executea string

    Groleo Marius wrote:[color=blue]
    > I have the folowing code:
    > c=2
    > e=3
    > s="12"
    > code.append('fu ntion ("c, e,s")')
    > print "\n".join(c ode) + '\n'
    >
    > The problem is when i call append.
    > The variables s, c, e are not replaced with their value, so in the end
    > i get the result:
    >
    > funtion(" c, e, s");
    >
    > The result that i want is :
    > funtion("2,3,12 ");
    >
    >
    >[/color]

    I don't know exactly what you want, but try this:
    [color=blue][color=green][color=darkred]
    >>> code.append('fu ntion ("%s, %s,%s")' % (c, e, s))
    >>> print "\n".join(c ode) + '\n'[/color][/color][/color]
    funtion ("2, 3,12")


    HTH,

    Arjen

    Comment

    • Cyril BAZIN

      #3
      Re: executea string

      > > c=2[color=blue][color=green]
      > > e=3
      > > s="12"
      > > code.append('fu ntion ("c, e,s")')
      > > print "\n".join(c ode) + '\n'
      > >[/color][/color]

      Hello,

      You could try this:

      -----------------------
      from string import Template
      code = ""
      c=2
      e=3
      s="12"
      code.append(Tem plate('function ("$c, $e, $s")').substitu te(vars()))
      print "\n".join(c ode) + '\n'
      ------------------------

      For more informations about templates, see:

      Note that is new in version 2.4...


      Cyril

      Comment

      Working...