简体   繁体   English

Python sys.argv从命令行自动转义我的参数?

[英]Python sys.argv auto-escaped my arguments from command line?

I am trying to build a script needs the power from 'grep' in linux shell. 我试图建立一个脚本,需要linux shell中'grep'的支持。
However, I found Python sys.argv escaped my regex pattern from command line. 但是,我发现Python sys.argv从命令行转义了我的regex模式。
For example, I am going to pass '\\d' as grep pattern from command line. 例如,我要从命令行传递'\\ d'作为grep模式。
But the string returned by sys.argv[1] was escaped as '\\\\d' 但是sys.argv [1]返回的字符串被转义为'\\\\ d'

I did the following test under debug mode: 我在调试模式下进行了以下测试:

#SyntaxTest.py
import sys
  #other stuff preventing my debugger from stopping after importing

#shell
python3 -m pdb SyntaxTest.py '\d'

> /cygdrive/d/PythonSandBox/SyntaxTest.py(1)<module>()
-> import sys
(Pdb) n
(Pdb) sys.argv[1]
'\\d'
(Pdb) print(sys.argv[1])
\d

I wonder why Python needs to escape my command line argument and I am asking a way to get a plain, un-escaped string in my example. 我想知道为什么Python需要转义我的命令行参数,并且在我的示例中要求一种获取纯净的,未转义的字符串的方法。

EDIT 编辑

It's impressive that '\\\\d'=='\\d'. '\\\\ d'=='\\ d'令人印象深刻。
But in my circumstance, I need to pass the string into subprocess like this. 但是在我的情况下,我需要像这样将字符串传递给子进程。

>>> pattern = '\d'
>>> str = r"echo '%s'"%pattern
>>> str
"echo '\\d'"
>>> subprocess.check_output(str,shell=True)
b'\\d\n'

It's obvious that \\\\d has been passed to shell. 显然\\\\ d已传递给shell。 However, I need it to be just \\d instead of \\\\d. 但是,我需要它只是\\ d而不是\\\\ d。 Is there any way other than substitute manually? 除了手动替代之外,还有其他方法吗?

That's just how the Python interpreter prints strings. 这就是Python解释器打印字符串的方式。 Your print call shows that the string isn't really escaped. 您的print呼叫显示该字符串并未真正转义。

>>> '\d'
'\\d'
>>> '\\d'
'\\d'
>>> print('\d')
\d
>>> print('\\d')
\d

Notice also that '\\d' and '\\\\d' are two ways of writing the same string. 还要注意, '\\d''\\\\d'是编写同一字符串的两种方式。

>>> '\d' == '\\d'
True

Python hasn't escaped your shell argument. Python没有逃脱您的shell参数。 This is how the backslash character is represented because backslash is the escape character, so in order to be literally used in the string, it has to be escaped. 这是反斜杠字符的表示方式,因为反斜杠是转义字符,所以为了在字符串中字面意义使用 ,它必须被转义。

For example, you must be aware that print '\\n' will print a newline character 例如,您必须知道print '\\n'将打印换行符

// Python REPL:
>>> print 'a\nb'
a
b

If you want to print the literal \\n string, you must escape the backslash character with another backslash: 如果要打印文字 \\ n字符串,则必须用另一个反斜杠转义反斜杠字符:

// Python REPL:
>>> print 'a\\nb'
a\nb

So a doubled backslash is just the way that a literal backslash is represented in a Python string (and I guess in almost all programming languages, as this is sort of a standard). 因此,加倍的反斜杠只是在Python字符串中表示文字反斜杠的方式(我想几乎所有的编程语言都采用这种斜杠,因为这是一种标准)。

For more information see https://en.wikipedia.org/wiki/Escape_character 有关更多信息,请参见https://en.wikipedia.org/wiki/Escape_character

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM