【发布时间】:2017-03-16 14:10:42
【问题描述】:
我需要的输出是:
'grep "key\":\"value" test_File.txt'
例如,我尝试的是..
>>> cmd = 'grep "key\":\"value" test_File.txt'
>>> cmd
'grep "key":"value" test_File.txt'
但当我尝试转义实际斜杠时,它会打印两个斜杠。
>>> cmd = 'grep "key\\":\\"value" test_File.txt'
>>> cmd
'grep "key\\":\\"value" test_File.txt'
>>>
我需要的是,我怎样才能得到第一行作为输出?
【问题讨论】:
-
只是
print(cmd)。您正在打印字符串的repr。 -
你到底想在这里完成什么?这看起来很像您试图打印要由 shell 执行的命令,并且对 shell 的引用规则感到非常困惑。一个有效的
grep命令看起来像grep '"key":"value"' test_File.txt,但我在这里猜测几个细节。使用subprocess,您希望避免使用shell=True,只传递['grep', '"key":"value"', 'test_File.txt']而不是字符串。 -
是的,我正在尝试将此作为命令传递给子进程以执行,
标签: python python-2.7