Python将值赋给包含(“{”和“字符”的变量

时间:2013-12-05 17:56:42

标签: 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": "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

提前谢谢。

UPDATE01

三重引号字符串确实允许我分配值但子代理不起作用

>>> cmd = """curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "e91a0ffe758c194f0d1d5896eb4daed0", "widget": "329bdbea887ad8e10e4e496f7a60f898", "title": "Something", "items":[{"label": "BOUGHT BREAD FOR", "value": "$999.99"}, {"label": "SOLD WATER FOR", "value": "$9,001.00"}] }' http://collector.superviso.com """
>>> subprocess.call(cmd)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 486, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 672, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1201, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

UPDATE02

感谢Adam的帖子(见上文),我能够在不指定任何变量的情况下查询请求:

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'
    ])

3 个答案:

答案 0 :(得分:3)

即使没有特殊字符,这也不起作用:

>>> cmd = curl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'curl' is not defined

您正在尝试创建一个字符串,因此您必须使用字符串文字语法的某些变体:

>>> cmd = 'curl'

这使得空格可以接受,并且双引号:

>>> 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"..."""

答案 1 :(得分:3)

subprocess.call也会获取一个参数列表,而不是一个单独的参数字符串。请参阅subprocess documentation。使用它要比担心正确引用要容易得多。例如:

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'
    ])

答案 2 :(得分:2)

你想要一个原始的三重双引号字符串r“”“blah”“”。

有关详细信息,请参阅此How to include a quote in a raw Python string?