What is the best way to handle a command line argument that includes an escape sequence like \n?

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

    What is the best way to handle a command line argument that includes an escape sequence like \n?

    I'm using Python 2.4 on Windows XP SP2.

    I'm trying to receive a command line argument that is a newline (\n)

    Here is the command line to use

    sample.py "\n"

    Here is a sample.py script

    import sys

    c = sys.argv[1]

    # when run c is set to \\n instead of \n.

    I created a test batch file

    echo %1

    to confirm that it was not the cmd.exe command processor causing the issue.

    It appears that Python treats the comand line string as a raw string.

    Is this a bug? If not what is the best way to work around the issue?

    Obviously I could use a hack

    if c == '\\n':
    c = '\n'

    But surely there is a better way.

    NOTE that I used \n in my sample but I also want to support other escape
    sequences too.



  • Steven Bethard

    #2
    Re: What is the best way to handle a command line argument that includesan escape sequence like \n?

    Joe wrote:[color=blue]
    > It appears that Python treats the comand line string as a raw string.
    >
    > what is the best way to work around the issue?[/color]

    You probably want to use str.decode with the encoding 'string_escape'[1]

    py> s = r'\n\t'
    py> s
    '\\n\\t'
    py> s.decode('strin g_escape')
    '\n\t'

    STeVe

    [1]http://docs.python.org/lib/standard-encodings.html

    Comment

    • Joe

      #3
      Re: What is the best way to handle a command line argument that includes an escape sequence like \n?

      Steve,

      THANKS! That is exactly what I was looking for but unable to find.

      Joe

      "Steven Bethard" <steven.bethard @gmail.com> wrote in message
      news:ocadne0P2f h3xrvfRVn-rQ@comcast.com. ..[color=blue]
      > Joe wrote:[color=green]
      >> It appears that Python treats the comand line string as a raw string.
      >>
      >> what is the best way to work around the issue?[/color]
      >
      > You probably want to use str.decode with the encoding 'string_escape'[1]
      >
      > py> s = r'\n\t'
      > py> s
      > '\\n\\t'
      > py> s.decode('strin g_escape')
      > '\n\t'
      >
      > STeVe
      >
      > [1]http://docs.python.org/lib/standard-encodings.html[/color]


      Comment

      • Steve Holden

        #4
        Re: What is the best way to handle a command line argument that includesan escape sequence like \n?

        Joe wrote:[color=blue]
        > I'm using Python 2.4 on Windows XP SP2.
        >
        > I'm trying to receive a command line argument that is a newline (\n)
        >
        > Here is the command line to use
        >
        > sample.py "\n"
        >
        > Here is a sample.py script
        >
        > import sys
        >
        > c = sys.argv[1]
        >
        > # when run c is set to \\n instead of \n.
        >
        > I created a test batch file
        >
        > echo %1
        >
        > to confirm that it was not the cmd.exe command processor causing the issue.
        >
        > It appears that Python treats the comand line string as a raw string.
        >
        > Is this a bug? If not what is the best way to work around the issue?
        >
        > Obviously I could use a hack
        >
        > if c == '\\n':
        > c = '\n'
        >
        > But surely there is a better way.
        >
        > NOTE that I used \n in my sample but I also want to support other escape
        > sequences too.
        >
        >
        >[/color]
        I don't want you getting more confused rather than less - newcomers are
        sometimes confused by Python's encoding of backslashes and such when
        printing out strings. What is your evidence for the assertion that c is
        set to \\n rather than \n?

        In Unix, it's easier to avoid this, since the shell lets you enter
        multi-line strings (note that these lines may wrap in the mail):

        [sholden@headrat sholden]$ python -c "import sys; print
        repr(sys.argv[1])" "\n"
        '\\n'
        [sholden@headrat sholden]$ python -c "import sys; print repr(sys.argv[1])" "[color=blue]
        > "[/color]
        '\n'

        The first case simply demonstrates that the shell doesn't interpret
        backslash escape sequences. I used repr() because that's what the
        interpreter will print if you enter an expression at the interactive
        prompt. The first case shows that the argument is two characters - you
        can verify this using the len() function.

        The second case shows how (with a sensible command shell) you can
        provide a newline as an argument.

        In Windows we can emulate the first case exactly:
        C:\Steve>C:\pyt hon24\python -c "import sys; print repr(sys.argv[1])" "\n"
        '\\n'

        Unfortunately Windows XP's command interpreter doesn't bother to wait
        until you close a double-quote left open at the end of a line:

        C:\Steve>\pytho n24\python -c "import sys; print repr(sys.argv[1])" "
        ''

        So someone else will have to tell you how to do that, but you should be
        clear about what's happening before you try and correct the problem.

        regards
        Steve
        --
        Meet the Python developers and your c.l.py favorites March 23-25
        Come to PyCon DC 2005 http://www.pycon.org/
        Steve Holden http://www.holdenweb.com/

        Comment

        • Antoon Pardon

          #5
          Re: What is the best way to handle a command line argument that includes an escape sequence like \n?

          Op 2005-03-02, Joe schreef <JoeSalmeri@hot mail.com>:[color=blue]
          > I'm using Python 2.4 on Windows XP SP2.
          >
          > I'm trying to receive a command line argument that is a newline (\n)
          >
          > Here is the command line to use
          >
          > sample.py "\n"[/color]

          Are you sure this supplies a newline and not the string <backslach> <n>
          [color=blue]
          > Here is a sample.py script
          >
          > import sys
          >
          > c = sys.argv[1]
          >
          > # when run c is set to \\n instead of \n.
          >
          > I created a test batch file
          >
          > echo %1
          >
          > to confirm that it was not the cmd.exe command processor causing the issue.[/color]

          I'm not sure this confirms anything. IMO it is possible that echo
          will translate <backslach> <n> to <newline>, giving you the
          impression that you have provideded a newline on the command line
          while in fact you have not.

          --
          Antoon Pardon

          Comment

          • Joe

            #6
            Re: What is the best way to handle a command line argument that includes an escape sequence like \n?

            Hi Steve,

            I've been using Python for many years, just hadn't had to deal with an
            escape sequence in the command line args. :-) and couldn't find the solution
            in the docs.

            It is easy to prove my assertion:

            import sys
            c = sys.argv[1]
            print len(c)
            print 'Line 1', c, 'Line 2'

            Output:
            2
            Line 1 \n Line 2

            Versus:

            import sys
            c = sys.argv[1]
            print len(c)
            print 'Line 1', c.decode('strin g_escape'), 'Line 2'

            Output:
            2
            Line 1
            Line 2

            Agreed, it is much easier on Unix to deal with this, I have not seen a way
            to get the XP command interpreter to allow a multiline command line as you
            described.

            Your solution was exactly what I need. I had an escape sequence entered on
            the command line and needed to decode the string so that Python used it as
            an escape sequence, in fact the sequence really is part of the output that
            the program produces.

            Thanks again for your assistance.

            Regards,

            Joe
            [color=blue]
            > I don't want you getting more confused rather than less - newcomers are
            > sometimes confused by Python's encoding of backslashes and such when
            > printing out strings. What is your evidence for the assertion that c is
            > set to \\n rather than \n?
            >
            > In Unix, it's easier to avoid this, since the shell lets you enter
            > multi-line strings (note that these lines may wrap in the mail):
            >
            > [sholden@headrat sholden]$ python -c "import sys; print repr(sys.argv[1])"
            > "\n"
            > '\\n'
            > [sholden@headrat sholden]$ python -c "import sys; print repr(sys.argv[1])"
            > "[color=green]
            > > "[/color]
            > '\n'
            >
            > The first case simply demonstrates that the shell doesn't interpret
            > backslash escape sequences. I used repr() because that's what the
            > interpreter will print if you enter an expression at the interactive
            > prompt. The first case shows that the argument is two characters - you can
            > verify this using the len() function.
            >
            > The second case shows how (with a sensible command shell) you can provide
            > a newline as an argument.
            >
            > In Windows we can emulate the first case exactly:
            > C:\Steve>C:\pyt hon24\python -c "import sys; print repr(sys.argv[1])" "\n"
            > '\\n'
            >
            > Unfortunately Windows XP's command interpreter doesn't bother to wait
            > until you close a double-quote left open at the end of a line:
            >
            > C:\Steve>\pytho n24\python -c "import sys; print repr(sys.argv[1])" "
            > ''
            >
            > So someone else will have to tell you how to do that, but you should be
            > clear about what's happening before you try and correct the problem.
            >
            > regards
            > Steve
            > --
            > Meet the Python developers and your c.l.py favorites March 23-25
            > Come to PyCon DC 2005 http://www.pycon.org/
            > Steve Holden http://www.holdenweb.com/[/color]


            Comment

            • Joe

              #7
              Re: What is the best way to handle a command line argument that includes an escape sequence like \n?

              Antoon,

              I tested the batch file :-)

              The one line batchfile does prove it because it prints out <backslash><n >
              and not <newline>.

              See other post, decode is exactly what was needed to fix the problem.

              Regards,

              Joe

              "Antoon Pardon" <apardon@forel. vub.ac.be> wrote in message
              news:slrnd2diqg .d87.apardon@rc pc42.vub.ac.be. ..[color=blue]
              > Op 2005-03-02, Joe schreef <JoeSalmeri@hot mail.com>:[color=green]
              >> I'm using Python 2.4 on Windows XP SP2.
              >>
              >> I'm trying to receive a command line argument that is a newline (\n)
              >>
              >> Here is the command line to use
              >>
              >> sample.py "\n"[/color]
              >
              > Are you sure this supplies a newline and not the string <backslach> <n>
              >[color=green]
              >> Here is a sample.py script
              >>
              >> import sys
              >>
              >> c = sys.argv[1]
              >>
              >> # when run c is set to \\n instead of \n.
              >>
              >> I created a test batch file
              >>
              >> echo %1
              >>
              >> to confirm that it was not the cmd.exe command processor causing the
              >> issue.[/color]
              >
              > I'm not sure this confirms anything. IMO it is possible that echo
              > will translate <backslach> <n> to <newline>, giving you the
              > impression that you have provideded a newline on the command line
              > while in fact you have not.
              >
              > --
              > Antoon Pardon[/color]


              Comment

              • Steve Holden

                #8
                Re: What is the best way to handle a command line argument that includesan escape sequence like \n?

                Joe wrote:[color=blue]
                > Hi Steve,
                >
                > I've been using Python for many years, just hadn't had to deal with an
                > escape sequence in the command line args. :-) and couldn't find the solution
                > in the docs.
                >
                > It is easy to prove my assertion:
                >
                > import sys
                > c = sys.argv[1]
                > print len(c)
                > print 'Line 1', c, 'Line 2'
                >
                > Output:
                > 2
                > Line 1 \n Line 2
                >[/color]
                The "2" appears to prove *my* assertion that the argument passed by the
                shell to your Python program consists of 2 characters, "\\" followed by "n".
                [color=blue]
                > Versus:
                >
                > import sys
                > c = sys.argv[1]
                > print len(c)
                > print 'Line 1', c.decode('strin g_escape'), 'Line 2'
                >
                > Output:
                > 2
                > Line 1
                > Line 2
                >[/color]
                As does this.
                [color=blue]
                > Agreed, it is much easier on Unix to deal with this, I have not seen a way
                > to get the XP command interpreter to allow a multiline command line as you
                > described.
                >
                > Your solution was exactly what I need. I had an escape sequence entered on
                > the command line and needed to decode the string so that Python used it as
                > an escape sequence, in fact the sequence really is part of the output that
                > the program produces.
                >[/color]
                In fairness it was Steven Bethard's solution that gave you the solution
                you needed. As long as ytour problem is solved, that's fine, and it
                appears that you've solved it in a reasonably cross-platform way.
                [color=blue]
                > Thanks again for your assistance.
                >[/color]
                Always happy to help when I can!

                regards
                Steve
                --
                Meet the Python developers and your c.l.py favorites March 23-25
                Come to PyCon DC 2005 http://www.pycon.org/
                Steve Holden http://www.holdenweb.com/

                Comment

                • Joe

                  #9
                  Re: What is the best way to handle a command line argument that includes an escape sequence like \n?

                  Hey no fair changing last names in the middle of a thread :-)

                  Thanks to BOTH Steve's.

                  [color=blue]
                  > In fairness it was Steven Bethard's solution that gave you the solution
                  > you needed. As long as ytour problem is solved, that's fine, and it
                  > appears that you've solved it in a reasonably cross-platform way.
                  >
                  > Steve Holden http://www.holdenweb.com/[/color]


                  Comment

                  Working...