简体   繁体   English

如何在Python中的字符串中双引号内显示​​单引号

[英]How to display single quotes inside double quotes in string in python

I know this is very basic question, but not able to frame it out. 我知道这是一个非常基本的问题,但无法解决。

I need to execute a customized python command based command with in a script. 我需要在脚本中执行基于定制python命令的命令。

ex:
below is the actual commands.
command-dist --host='server1' -- cmd -- 'command1;command2'

I need to use this command in my python script form my script , this command needs be to executed on remote server 我需要在我的python脚本形式的脚本中使用此命令,此命令需要在远程服务器上执行

I need this command format to use in my script 我需要在脚本中使用此命令格式

cmd="ssh -q %s "command-dist --host=%s -- cmd -- 'cmd1;cmd2'"" %(host1,host2)

but the above is failing due to quotes, tried number of ways still no luck. 但是以上由于引号而失败,尝试过的方法仍然没有运气。

When I tried this way
cmd="ssh -q %s \"command-dist --host=%s -- cmd -- 'cmd1;cmd2'\"" %(host1,host2)

I don't under why back slashes are appearing before and after cmd1;cmd2?
This is what I am not getting.
cmd
'ssh -q %s "command-dist --host=%s -- cmd -- /'cmd1;cmd2'/""' %(host1,host2)

Please help how do I get the above value 请帮助我如何获得上述价值

This 这个

cmd="ssh -q %s "command-dist --host=%s -- cmd -- 'cmd1;cmd2'"" %(host1,host2)

is understood by Python as 被Python理解为

cmd = <some string>command-dist --host=%s -- cmd -- 'cmd1;cmd2'<some string>

because you use the same quotes inside and outside. 因为您在内部和外部使用相同的引号。 Try instead to escape the internal quotes with a backslash: 尝试改用反斜杠转义内部引号:

cmd="ssh -q %s \"command-dist --host=%s -- cmd -- 'cmd1;cmd2'\"" %(host1,host2)

Aside from that, you should use the subprocess module and supply your command as a list to subprocess.Popen . 除此之外,您应该使用subprocess模块并将命令作为list列表提供给subprocess.Popen

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

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