backslash plague

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luis P. Mendes

    #1

    backslash plague

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    hi,

    I've already read many pages on this but I'm not able to separate the
    string 'R0\1.2646\1.26 49\D' in four elements, using the \ as the separator.

    a='R0\1.2644\1. 2344\D'
    re.sub(r'\'','f f',a) does nothing
    and why must I write two '' after the \? If I hadn't used r I would
    understand...

    how should I do it?

    Luis
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFBeVzlHn4 UHCY8rB8RAqa7AJ oDbHIXje4yP/pTZpOH0ZVe1MGqw wCfadOa
    T8GTyeJU6Jve140 5Xa9cuus=
    =P+kP
    -----END PGP SIGNATURE-----
  • Alex Martelli

    #2
    Re: backslash plague

    Luis P. Mendes <luisXX_lupe2XX @netvisaoXX.pt> wrote:
    ...[color=blue]
    > I've already read many pages on this but I'm not able to separate the
    > string 'R0\1.2646\1.26 49\D' in four elements, using the \ as the separator.[/color]

    x = r'R0\1.2646\1.2 649\D'
    elements = x.split('\\')
    [color=blue]
    > and why must I write two '' after the \? If I hadn't used r I would
    > understand...[/color]

    A raw literal can't end with an odd number of backslashes (_some_ way
    has to be there to escape the quote char, after all).
    [color=blue]
    > how should I do it?[/color]

    I think the string's split method, as above, is the simplest, fastest
    way.


    Alex

    Comment

    • Luis P. Mendes

      #3
      Re: backslash plague

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      So, the trick is to put the r in front of the string?!

      with the r in front:

      x = r'R0\1.2646\1.2 649\D'
      |>> elements = x.split('\\')
      |>> elements
      ['R0', '1.2646', '1.2649', 'D'] <--what I want


      without it:

      y='R0\1.2646\1. 2649\D'
      |>> elements = y.split('\\')
      |>> elements
      ['R0\x01.2646\x0 1.2649', 'D'] <-- not good


      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.2.4 (GNU/Linux)
      Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

      iD8DBQFBeWMjHn4 UHCY8rB8RArvEAJ 91mc423u5zY+xYQ OLCXoE3ouzb4gCe Pix/
      a745y1s3UMvW61p rb6ndCUk=
      =pREV
      -----END PGP SIGNATURE-----

      Comment

      • Mike Rovner

        #4
        Re: backslash plague

        Luis P. Mendes wrote:
        [color=blue]
        > I've already read many pages on this but I'm not able to separate the
        > string 'R0\1.2646\1.26 49\D' in four elements, using the \ as the separator.
        >
        > a='R0\1.2644\1. 2344\D'[/color]

        a=r'R0\1.2644\1 .2344\D'
        [color=blue]
        > re.sub(r'\'','f f',a) does nothing[/color]
        [color=blue][color=green][color=darkred]
        >>> re.sub(r'\\\\', 'ff',a)[/color][/color][/color]
        'R0ff1.2644ff1. 2344ffD'

        and also
        [color=blue][color=green][color=darkred]
        >>> a.replace('\\', 'ff')[/color][/color][/color]
        'R0ff1.2644ff1. 2344ffD'

        You said, you wanna _split_ them:
        [color=blue][color=green][color=darkred]
        >>> a.split('\\')[/color][/color][/color]
        ['R0', '1.2644', '1.2344', 'D']

        HTH,
        Mike

        Comment

        • Paul McGuire

          #5
          Re: backslash plague

          "Luis P. Mendes" <luisXX_lupe2XX @netvisaoXX.pt> wrote in message
          news:2tt4b2F24c ffeU1@uni-berlin.de...[color=blue]
          > -----BEGIN PGP SIGNED MESSAGE-----
          > Hash: SHA1
          >
          > hi,
          >
          > I've already read many pages on this but I'm not able to separate the
          > string 'R0\1.2646\1.26 49\D' in four elements, using the \ as the[/color]
          separator.[color=blue]
          >
          > a='R0\1.2644\1. 2344\D'
          > re.sub(r'\'','f f',a) does nothing
          > and why must I write two '' after the \? If I hadn't used r I would
          > understand...
          >
          > how should I do it?
          >
          > Luis[/color]

          Problem 1:
          Did you perhaps intend:
          a = r'R0\1.2644\1.2 344\D'
          Otherwise, your string contains this: "R0?.2644?.2344 \D" - the \1 sequence
          is interpreted to mean "ascii character 1", which is the ASCII <SOH>.

          Problem 2: "why must I write two '' after the \?"
          Even raw strings cannot handle a backslash as the final character, as this
          is interpreted as being an escaped quotation character. Your assignment to
          a above is a good candidate for raw strings, but futile for '\\' etc. (See
          next.)

          Problem 3:
          If you are trying to re.sub the delimiting backslashes, then you need to
          double-double them for re to process them correctly.

          Try this:

          a = r'R0\1.2644\1.2 344\D'
          bslash = '\\'
          re_bslash = '\\\\'
          print a.split( bslash )
          print re.sub( re_bslash, 'ff', a )

          gives:
          ['R0', '1.2644', '1.2344', 'D']
          R0ff1.2644ff1.2 344ffD


          -- Paul


          Comment

          • Russell Blau

            #6
            Re: backslash plague

            "Luis P. Mendes" <luisXX_lupe2XX @netvisaoXX.pt> wrote in message
            news:2tt4b2F24c ffeU1@uni-berlin.de...[color=blue]
            > -----BEGIN PGP SIGNED MESSAGE-----
            > Hash: SHA1
            >
            > hi,
            >
            > I've already read many pages on this but I'm not able to separate the
            > string 'R0\1.2646\1.26 49\D' in four elements, using the \ as the[/color]
            separator.[color=blue]
            >
            > a='R0\1.2644\1. 2344\D'[/color]

            Here's your first problem: the name "a" is not mapped to the same string you
            think it is. Python interprets the backslashes in a quoted string as escape
            sequences, unless you specify a raw string by putting an "r" before the
            first quotation mark. Specifically, it interprets the sequence \1 as the
            character with an ASCII value of 1. So what you've done is:
            [color=blue][color=green][color=darkred]
            >>> a = 'R0\1.2646\1.26 49\D'
            >>> print a[/color][/color][/color]
            R0?.2646?.2649\ D

            (The preceding line contains non-ASCII characters that may display oddly...)

            I think what you really want is:
            [color=blue][color=green][color=darkred]
            >>> a = r'R0\1.2646\1.2 649\D'
            >>> print a[/color][/color][/color]
            R0\1.2646\1.264 9\D

            Once you have stored your string properly, what's wrong with this?
            [color=blue][color=green][color=darkred]
            >>> print a.split("\\")[/color][/color][/color]
            ['R0', '1.2646', '1.2649', 'D']

            --
            I don't actually read my hotmail account, but you can replace hotmail with
            excite if you really want to reach me.


            Comment

            • Luis P. Mendes

              #7
              Re: backslash plague

              -----BEGIN PGP SIGNED MESSAGE-----
              Hash: SHA1

              thanks!

              I was just considering the effect of back slashes in the search/replace
              criterium and not in the string itself. Thank you again!

              Luis
              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.2.4 (GNU/Linux)
              Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

              iD8DBQFBeWpaHn4 UHCY8rB8RAtK+AJ 421wDXodQ0zCM9A oDuayojh+uFUQCe JPL2
              rBWMVP36h6SBKLv qcWecBw4=
              =R4+I
              -----END PGP SIGNATURE-----

              Comment

              • Alex Martelli

                #8
                Re: backslash plague

                Luis P. Mendes <luisXX_lupe2XX @netvisaoXX.pt> wrote:
                [color=blue]
                > So, the trick is to put the r in front of the string?![/color]

                If you want a literal string with backslashes in it, you either double
                each backslash or use a rawliteral (r in front).
                [color=blue]
                > y='R0\1.2646\1. 2649\D'[/color]

                the third character of y, \1, is in this case the byte with value 1 in
                the ASCII code, etc. Apparently that's not what you want.

                But is this string going to be a literal in your code, rather than, say,
                read from a file? Sounds unlikely. When you read from a file there's
                no escape-sequence interpretation, so the issue of how to write
                literals, raw or otherwise, is irrelevant there.


                Alex

                Comment

                • Bengt Richter

                  #9
                  Re: backslash plague

                  On Fri, 22 Oct 2004 21:20:30 +0200, aleaxit@yahoo.c om (Alex Martelli) wrote:
                  [color=blue]
                  >Luis P. Mendes <luisXX_lupe2XX @netvisaoXX.pt> wrote:
                  > ...[color=green]
                  >> I've already read many pages on this but I'm not able to separate the
                  >> string 'R0\1.2646\1.26 49\D' in four elements, using the \ as the separator.[/color]
                  >
                  >x = r'R0\1.2646\1.2 649\D'
                  >elements = x.split('\\')
                  >[color=green]
                  >> and why must I write two '' after the \? If I hadn't used r I would
                  >> understand...[/color]
                  >
                  >A raw literal can't end with an odd number of backslashes (_some_ way
                  >has to be there to escape the quote char, after all).
                  >[/color]
                  Hm, just had the thought that something analogous to HDLC bit-stuffing
                  could be used. IIRC bitstreams had escape flags composed of 5 successive bits,
                  and if you wanted to transmit 5 successive data bits, you just added an extra bit
                  at the end to make 6 to show that the five did not comprise a flag. The extra bits
                  would get dropped on decoding when a 6th 1 followed 11111 and would be recognized
                  as a flag otherwise.

                  Translating this to quoted character sequences, we could have an alternate triple
                  quoted raw string format, with quote-stuffing instead of escapes. I.e., to quote
                  three successive quote characters, we stuff a 4th quote, which the tokenizer drops
                  as it creates the internal byte sequence string representation, so we don't need
                  escapes in the usual sense.

                  Thus (using f prefix to indicate flagged quote-stuffing syntax) you could write:

                  x = f'''c:\whatever \'''

                  and to quote the line above (without taking advantage of alternate quotes):

                  q = f''' x = f''''c:\whateve r\'''''''
                  ^^^ ^^^| ^^^|^^^

                  where ^^^ is flag and | indicates a stuffed quote that
                  makes the previous otherwise-flag into three quotes in the data.
                  You could quote again (using same type quote for illustrative purposes
                  again, since oviously you could do better using both ' and "):

                  r = f'''f'''' x = f'''''c:\whatev er\'''''''''''
                  ^^^| ^^^| ^^^|^^^|^^^

                  (I think ;-)

                  I guess the worst-case data to quote would be a repeating pattern of
                  '''""" or """''' since neither type of quote character would give an
                  advantage, but 1-in-6 overhead is still not too bad, and it would be rare.

                  Is there a hole in this raw string quoting syntax?

                  Regards,
                  Bengt Richter

                  Comment

                  • Bengt Richter

                    #10
                    Re: backslash plague

                    On Sat, 23 Oct 2004 22:33:05 GMT, bokr@oz.net (Bengt Richter) wrote:
                    [color=blue]
                    >On Fri, 22 Oct 2004 21:20:30 +0200, aleaxit@yahoo.c om (Alex Martelli) wrote:
                    >[color=green]
                    >>Luis P. Mendes <luisXX_lupe2XX @netvisaoXX.pt> wrote:
                    >> ...[color=darkred]
                    >>> I've already read many pages on this but I'm not able to separate the
                    >>> string 'R0\1.2646\1.26 49\D' in four elements, using the \ as the separator.[/color]
                    >>
                    >>x = r'R0\1.2646\1.2 649\D'
                    >>elements = x.split('\\')
                    >>[color=darkred]
                    >>> and why must I write two '' after the \? If I hadn't used r I would
                    >>> understand...[/color]
                    >>
                    >>A raw literal can't end with an odd number of backslashes (_some_ way
                    >>has to be there to escape the quote char, after all).
                    >>[/color]
                    >Hm, just had the thought that something analogous to HDLC bit-stuffing
                    >could be used. IIRC bitstreams had escape flags composed of 5 successive bits,
                    >and if you wanted to transmit 5 successive data bits, you just added an extra bit
                    >at the end to make 6 to show that the five did not comprise a flag. The extra bits
                    >would get dropped on decoding when a 6th 1 followed 11111 and would be recognized
                    >as a flag otherwise.[/color]
                    BZZT! wrong ;-(
                    The flag is 01111110 and I believe 0 gets stuffed after the 5th 1 to make sure
                    the flag is not part of data between real flags.
                    [color=blue]
                    >
                    >Translating this to quoted character sequences, we could have an alternate triple
                    >quoted raw string format, with quote-stuffing instead of escapes. I.e., to quote
                    >three successive quote characters, we stuff a 4th quote, which the tokenizer drops
                    >as it creates the internal byte sequence string representation, so we don't need
                    >escapes in the usual sense.[/color]
                    This does not work for e.g. quoting as single quote, so it's not general at all :-(
                    [color=blue]
                    >
                    >Thus (using f prefix to indicate flagged quote-stuffing syntax) you could write:
                    >
                    > x = f'''c:\whatever \'''
                    >
                    >and to quote the line above (without taking advantage of alternate quotes):
                    >
                    > q = f''' x = f''''c:\whateve r\'''''''
                    > ^^^ ^^^| ^^^|^^^
                    >
                    >where ^^^ is flag and | indicates a stuffed quote that
                    >makes the previous otherwise-flag into three quotes in the data.
                    >You could quote again (using same type quote for illustrative purposes
                    >again, since oviously you could do better using both ' and "):
                    >
                    > r = f'''f'''' x = f'''''c:\whatev er\'''''''''''
                    > ^^^| ^^^| ^^^|^^^|^^^
                    >
                    >(I think ;-)
                    >
                    >I guess the worst-case data to quote would be a repeating pattern of
                    >'''""" or """''' since neither type of quote character would give an
                    >advantage, but 1-in-6 overhead is still not too bad, and it would be rare.
                    >
                    >Is there a hole in this raw string quoting syntax?
                    >[/color]
                    Unfortunately, yes.

                    I thought of another format, but it doesn't quote previously quoted arbitrary text
                    without modifying at least the last character, so phooey. Might as well go to the
                    previously suggested mime-style delimiting, which an editor macro could do for
                    arbitrary selected text. It could use str(time.time() ) as delimiter text without
                    much risk, IWT.

                    Regards,
                    Bengt Richter

                    Comment

                    Working...