python - 在双引号内添加变量,变量也包含双引号
Posted
技术标签:
【中文标题】python - 在双引号内添加变量,变量也包含双引号【英文标题】:python - add Variables inside double quotes, The Variables also contains double quotes 【发布时间】:2019-02-27 07:10:54 【问题描述】:我有原始内容,我需要如下所示的预期输出。
curl -X POST -H "Content-Type: application/json" https://bitbucket.org/api/2.0/repositories/username/BB_Access/pullrequests -d ' "title": "Merge dev branch to Master", "source": "branch": "name": "Dev" , "repository": "full_name": "username/BB_Access" , "destination": "branch": "name": "master" , "close_source_branch": false '
下面是我试过的代码。
import subprocess
BBrepo = "BB_Access"
BBuser = "username"
src_branch = "Dev"
Dest_branch = "master"
pull_command = "curl -X POST -H "'"Content-Type: application/json"'" https://bitbucket.org/api/2.0/repositories/"+BBuser+"/"+BBrepo+"/pullrequests -d '"' "title": "python_pull_request", "source": "branch": "name": '"' "+src_branch+" , '"'repository": "full_name": '"'"+BBuser+"/"+BBrepo+" , '"'destination": "branch": "name": '"'"+Dest_branch+'"'" , '"'close_source_branch": false '"'"
print pull_command
但它给出的输出如下
curl -X POST -H "Content-Type: application/json" https://bitbucket.org/api/2.0/repositories/username/BB_Access/pullrequests -d ' "title": "python_pull_request", "source": "branch": "name": ' Dev , 'repository": "full_name": 'username/BB_Access , 'destination": "branch": "name": 'master" , 'close_source_branch": false '
谁能建议我获得预期输出的更好方法。
【问题讨论】:
【参考方案1】:我看到了两种方式:
在''
内使用"
(或相反)
转义,如"\""
或'\''
【讨论】:
【参考方案2】:你可以试试这个:
import subprocess
BBrepo = "BB_Access"
BBuser = "username"
src_branch = "Dev"
Dest_branch = "master"
pull_command = 'curl -X POST -H "Content-Type: application/json" https://bitbucket.org/api/2.0/repositories/'+BBuser+'/'+BBrepo+'/pullrequests -d \' "title": "python_pull_request", "source": "branch": "name": '+'"'+src_branch+'"'+' , "repository": "full_name": '+'"'+BBuser+'/'+BBrepo+'"'+' , "destination": "branch": "name": '+'"'+Dest_branch+'"'+' , "close_source_branch": false \''
print(pull_command)
安得:
curl -X POST -H "Content-Type: application/json" https://bitbucket.org/api/2.0/repositories/username/BB_Access/pullrequests -d ' "title": "python_pull_request", "source": "branch": "name": "Dev" , "repository": "full_name": "username/BB_Access" , "destination": "branch": "name": "master" , "close_source_branch": false '
【讨论】:
完美运行以上是关于python - 在双引号内添加变量,变量也包含双引号的主要内容,如果未能解决你的问题,请参考以下文章