2016-12-03 129 views
0

我知道這是非常基本的問題,但無法將其框出。如何在python中的字符串中的雙引號內顯示​​單引號

我需要在腳本中執行一個自定義的基於python命令的命令。

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

我需要在我的Python腳本中使用此命令 形成我的腳本,這個命令需要是遠程服​​務器上執行

我需要這個命令的格式在我的腳本中使用

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

但以上是由於報價失敗,試過多少種方式仍然沒有運氣。

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) 

請幫助我如何獲得上述值

回答

0

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

由Python的理解爲

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

因爲你用內外相同的報價。 嘗試,而不是用反斜槓逃逸的內部報價:

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

除此之外,你應該使用subprocess模塊和一個列表subprocess.Popen提供您的命令。

相關問題