Python为包含(";&x27;{";和:字符的变量赋值
我有以下代码:Python为包含(";&x27;{";和:字符的变量赋值,python,curl,subprocess,Python,Curl,Subprocess,我有以下代码: import subprocess cmd = curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "e91a0ffe758c194f0d1d5896eb4daed0", "widget": "79c08a7e70f0253c3da2fab39e7cb89b", "title": "Something", "text": "Some text", "moreinfo": "Subti
import subprocess
cmd = curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "e91a0ffe758c194f0d1d5896eb4daed0", "widget": "79c08a7e70f0253c3da2fab39e7cb89b", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }' http://collector.superviso.com
subprocess.call(cmd)
我试图给一个变量赋值,这会导致语法错误。下面是发生的情况:
>>> cmd = curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth_token", "widget": "widget_id", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }' http://domain.com
File "<stdin>", line 1
cmd = curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth_token", "widget": "widget_id", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }' http://domain.com
^
SyntaxError: invalid syntax
您需要一个原始的三重双引号字符串r”““blah”“”
有关更多详细信息,请参见此您需要原始的三重双引号字符串r”““blah”“”
有关更多详细信息,请参阅此即使没有特殊字符也无法使用的:
>>> cmd = curl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'curl' is not defined
这样就可以接受空格和双引号:
>>> cmd = 'curl -X POST -H "Content-Type: application/json"'
但是,您不能在其中嵌套未替换的单引号。要解决此问题,您可以选择对内部单引号进行转义,或对整个字符串进行三重引用:
>>> cmd = 'curl -X post -H "Content-Type: application/json" -d \'{ "auth_token"...'
>>> cmd = """curl -X POST -H "Content-Type: application/json" -d '{ "auth_token"..."""
即使没有特殊字符也不起作用:
>>> cmd = curl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'curl' is not defined
这样就可以接受空格和双引号:
>>> cmd = 'curl -X POST -H "Content-Type: application/json"'
但是,您不能在其中嵌套未替换的单引号。要解决此问题,您可以选择对内部单引号进行转义,或对整个字符串进行三重引用:
>>> cmd = 'curl -X post -H "Content-Type: application/json" -d \'{ "auth_token"...'
>>> cmd = """curl -X POST -H "Content-Type: application/json" -d '{ "auth_token"..."""
subprocess.call
也采用参数列表,而不是单一的简单参数字符串。请参阅。使用它比担心正确引用要容易得多。例如:
subprocess.call([
'curl',
'-X',
'POST',
'-H',
'Content-Type: application/json',
'-d',
'{ "auth_token": "e91a0ffe758c194f0d1d5896eb4daed0", "widget": "79c08a7e70f0253c3da2fab39e7cb89b", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }',
'http://collector.superviso.com'
])
subprocess.call
也采用参数列表,而不是单一的简单参数字符串。请参阅。使用它比担心正确引用要容易得多。例如:
subprocess.call([
'curl',
'-X',
'POST',
'-H',
'Content-Type: application/json',
'-d',
'{ "auth_token": "e91a0ffe758c194f0d1d5896eb4daed0", "widget": "79c08a7e70f0253c3da2fab39e7cb89b", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }',
'http://collector.superviso.com'
])
+1.将
cmd
作为子进程调用:subprocess.check_调用(shlex.split(cmd))
+1.将cmd
作为子进程调用:subprocess.check_调用(shlex.split(cmd))
您还可以将此作为另一种创建字符串文字的方法的灵感,即使子流程
模块没有-'.join(['curl','-X',…])
我应该祝贺Adam注意到了这个方法。它解决了问题!!@user2843053:欢迎使用StackOverflow。如果这个方法解决了您的问题,您应该这样做。您还可以将此作为另一种方法的灵感来创建字符串文字,即使子流程
模块没有-”。join(['curl','-X',…])
我应该祝贺Adam注意到了这个方法。它解决了问题!!@user2843053:欢迎使用StackOverflow。如果这解决了您的问题,您应该。