带有双引号的子过程

问题描述 投票:0回答:2

在python中,必须在子进程中运行以下perl命令。

perl命令:

perl dcm.pl -r ona_sql sql="select something from table where name='somename'" header=no

我需要对引号进行转义,但无法使其正常工作。

下面的示例对我来说最有意义,但这也不起作用。

import subprocess
import sys

subprocess.check_output(["/usr/bin/perl", "/opt/ona/bin/dcm.pl", "-r", "ona_sql", "sql=\"select something from table where name='somename'\"", "header=no"]).decode(sys.stdout.encoding)

结果:

subprocess.CalledProcessError: Command '['/usr/bin/perl', '/opt/ona/bin/dcm.pl', '-r', 'ona_sql', 'sql="select something from table where name=\'somename\'"', 'header=no']' returned non-zero exit status 3
python subprocess
2个回答
0
投票

尝试改用单引号:

'sql="select something from table where name=\'somename\'"'

0
投票

您可以使用三引号将字符串文字包含在单引号和双引号中:

subprocess.check_output(['perl', '/opt/ona/bin/dcm.pl', '-r', 'ona_sql', '''sql="select something from table where name='somename'"''', 'header=no'])
© www.soinside.com 2019 - 2024. All rights reserved.