trouble using \ to escape %

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lucas Machado

    #1

    trouble using \ to escape %

    I'm writing a python script that modifies the smb.conf file, and i need
    to write the characters '%U' in the file. I tried making a string like
    so:

    str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%U" % (list[0], list[1],
    list[0], list[1])

    but i keep getting: "TypeError: not enough arguments for format
    string". I've also tried making the string:

    str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%" % (list[0], list[1],
    list[0], list[1])

    but i get: "ValueError : incomplete format". These errors lead me to
    believe that for some reason it is not escaping the '%' character.
    There has to be a way to write '%' to a file. Thanks in advance..

    Cheers
    -Lucas Machado

  • Rob Williscroft

    #2
    Re: trouble using \ to escape %

    Lucas Machado wrote in news:1113606334 .524947.54630
    @l41g2000cwc.go oglegroups.com in comp.lang.pytho n:
    [color=blue]
    > str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%U" % (list[0], list[1],
    > list[0], list[1])
    >[/color]

    In a format string use '%%' for a single % char':

    str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/%%U" % (list[0], list[1],list[0],
    list[1])


    Rob.
    --

    Comment

    • Maarten Sneep

      #3
      Re: trouble using \ to escape %

      In article <1113606334.524 947.54630@l41g2 000cwc.googlegr oups.com>,
      "Lucas Machado" <LMachado1@gmai l.com> wrote:
      [color=blue]
      > I'm writing a python script that modifies the smb.conf file, and i need
      > to write the characters '%U' in the file.[/color]

      print "%s = %%U" % "a" yields a = %U for me.

      Maarten

      Comment

      • Dan Bishop

        #4
        Re: trouble using \ to escape %

        Lucas Machado wrote:[color=blue]
        > I'm writing a python script that modifies the smb.conf file, and i[/color]
        need[color=blue]
        > to write the characters '%U' in the file. I tried making a string[/color]
        like[color=blue]
        > so:
        >
        > str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%U" % (list[0], list[1],
        > list[0], list[1])
        >
        > but i keep getting: "TypeError: not enough arguments for format
        > string". I've also tried making the string:
        >
        > str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%" % (list[0], list[1],
        > list[0], list[1])
        >
        > but i get: "ValueError : incomplete format". These errors lead me to
        > believe that for some reason it is not escaping the '%' character.
        > There has to be a way to write '%' to a file. Thanks in advance..[/color]
        [color=blue][color=green][color=darkred]
        >>> percentage = 93
        >>> # The string % operator uses "%%" for the percent sign.
        >>> format_string = "%d%% of all statistics are made up on the spot."
        >>> print format_string % percentage[/color][/color][/color]
        93% of all statistics are made up on the spot.[color=blue][color=green][color=darkred]
        >>> # When the % operator is not used, "%" is just another character.
        >>> print format_string[/color][/color][/color]
        %d%% of all statistics are made up on the spot.[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Comment

        • John Machin

          #5
          Re: trouble using \ to escape %

          On 15 Apr 2005 16:05:34 -0700, "Lucas Machado" <LMachado1@gmai l.com>
          wrote:
          [color=blue]
          >I'm writing a python script that modifies the smb.conf file, and i need
          >to write the characters '%U' in the file. I tried making a string like
          >so:
          >
          >str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%U" % (list[0], list[1],
          >list[0], list[1])
          >
          >but i keep getting: "TypeError: not enough arguments for format
          >string". I've also tried making the string:
          >
          >str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%" % (list[0], list[1],
          >list[0], list[1])
          >
          >but i get: "ValueError : incomplete format". These errors lead me to
          >believe that for some reason it is not escaping the '%' character.
          >There has to be a way to write '%' to a file. Thanks in advance..[/color]

          Looks like you've already got your answer, but here's some META-help:
          what to do when you have a problem and your internet connection is
          broken:

          1. GUESS. Extrapolation from examples like DLE DLE in networking, $$
          in m4 and \\ in *almost* everything might suggest trying %%

          2. READ THE DOCS. Don't know what platform you're running on, but one
          of what a recent blogger called "hopeless Windows people" [like me]
          would do is this:

          Start->Programs->Python 2.4->Python manuals, click on the Index tab,
          type %, hit Enter ... scrolling down leads to a table of conversion
          types, the last of which is the somewhat tautological """% No argument
          is converted, results in a "%" character in the result."""

          HTH,
          John

          Comment

          Working...