在命令行参数中解析\ - python 2.7.3

时间:2013-02-16 16:58:27

标签: python bash parsing command-line-arguments

我从bash

调用python脚本parse_input.py

parse_input.py采用命令行参数,其中包含许多'\n'个字符。

示例输入:

$ 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

我可以在pdb上看到`data = "1\\n2\\n",而我想要data="1\n2\n"

我看到只有\(没有\n)的类似行为被\\取代

如何删除额外的\

我不希望脚本处理额外的\ 也可以从文件中接收相同的输入。

bash版:GNU bash,版本4.2.24(1)-release(i686-pc-linux-gnu)

python版本:2.7.3

2 个答案:

答案 0 :(得分:8)

Bash没有像python那样解释\n,它认为它是两个字符。

可以通过\n中的'解码'将文字string_escape(所以两个字符)解释为python中的换行符:

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

请注意,其他python string escape sequences 将被解释。

答案 1 :(得分:8)

Bash不会解释常规单引号和双引号字符串中的转义字符。要使其解释(某些)转义字符,您可以使用$'...'

   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'