Analysis in command line argument - python 2.7.3

I am calling a python script, parse_input.pyfrom bash

parse_input.pyaccepts a command line argument that has many '\n'characters.

Input Example:

$ python parse_input.py "1\n2\n"

import sys
import pdb

if __name__ == "__main__":

    assert(len(sys.argv) == 2)

    data =  sys.argv[1]
    pdb.set_trace()
    print data

I can see on pdb that `data = "1\\n2\\n", then how I wantdata="1\n2\n"

I saw similar behavior only with \(without \n) which is replaced by\\

How to remove an extra \?

I do not want the script to deal with additional \as the same input can also be obtained from the file.

bash version : GNU bash version 4.2.24 (1) -release (i686-pc-linux-gnu)

python version : 2.7.3

+5
source share
2 answers

Bash escape- . () escape-, $'...':

   Words of the form $'string' are treated specially.  The word expands to
   string, with backslash-escaped characters replaced as specified by  the
   ANSI  C  standard.  Backslash escape sequences, if present, are decoded
   as follows:
          \a     alert (bell)
          \b     backspace
          \e     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \\     backslash
          \'     single quote
          \nnn   the eight-bit character whose value is  the  octal  value
                 nnn (one to three digits)
          \xHH   the  eight-bit  character  whose value is the hexadecimal
                 value HH (one or two hex digits)
          \cx    a control-x character

   The expanded result is single-quoted, as if the  dollar  sign  had  not
   been present.

.

$ python parse_input.py $'1\n2\n'
+7

Bash \n python, .

\n ( ) python "" string_escape:

data = data.decode('string_escape')

:

>>> literal_backslash_n = '\\n'
>>> len(literal_backslash_n)
2
>>> literal_backslash_n.decode('string_escape')
'\n'
>>> len(literal_backslash_n.decode('string_escape'))
1

, escape- python .

+8

All Articles