Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在单引号中传递Python列表_Python_Bash_Shell_Docopt - Fatal编程技术网

在单引号中传递Python列表

在单引号中传递Python列表,python,bash,shell,docopt,Python,Bash,Shell,Docopt,我有一段如下的代码,用于向API调用发送IP地址列表 body = {'cID': id, 'dbType': params['db-type'].upper(), 'cidrList': eval(params['--cidr-list'])} print(json.dumps(body)) conn.request("POST", "/Link/to/API", body=json.dumps(body), headers=header) check_resp(200) logger.inf

我有一段如下的代码,用于向API调用发送IP地址列表

body = {'cID': id, 'dbType': params['db-type'].upper(), 'cidrList': eval(params['--cidr-list'])}
print(json.dumps(body))
conn.request("POST", "/Link/to/API", body=json.dumps(body), headers=header)
check_resp(200)
logger.info("Rules changed successfully")
但是,当我使用下面的参数调用此代码时,它失败了

--cidr-list ['10.20.0.0/32','10.30.0.0/32']
当我使用下面的选项时,它会起作用

--cidr-list [\"10.20.0.0/32\",\"10.30.0.0/32\"]

所以基本上当我使用
\“
要包装列表中的每个项目,将其解析为单引号。如何更改代码以使其接受输入1?我是Python新手,如果您能解释一下Python背后的逻辑,我将不胜感激。提前感谢。

您需要转义单引号,因为shell也使用单引号

--cidr-list [\'10.20.0.0/32\',\'10.30.0.0/32\']
最好的方法是把整个论点用引号引起来

--cidr-list "['10.20.0.0/32','10.30.0.0/32']"
or
--cidr-list '["10.20.0.0/32","10.30.0.0/32"]'

顺便说一句,您应该使用
ast.literal\u eval()
而不是
eval()
,您需要转义单引号,因为shell也使用单引号

--cidr-list [\'10.20.0.0/32\',\'10.30.0.0/32\']
最好的方法是把整个论点用引号引起来

--cidr-list "['10.20.0.0/32','10.30.0.0/32']"
or
--cidr-list '["10.20.0.0/32","10.30.0.0/32"]'

顺便说一句,您应该使用
ast.literal\u eval()
而不是
eval()

不要将Python知识作为使用程序的必要条件

body = {
    'cID': id,
    'dbType': params['db-type'].upper(),
    'cidrList': params['--cidr-list'].split(',')
}
print(json.dumps(body))
conn.request("POST", "/Link/to/API", json=body, headers=header)
check_resp(200)
logger.info("Rules changed successfully")
然后使用

script.py ... --cidr-list 10.20.0.0/32,10.30.0.0/32

所有参数都已经是字符串;您不需要对用户强制使用Python字符串文字语法,逗号分隔的字符串足以处理成CIDR地址列表,而无需对用户强制使用Python列表语法。

不要将了解Python作为使用程序的必要条件

body = {
    'cID': id,
    'dbType': params['db-type'].upper(),
    'cidrList': params['--cidr-list'].split(',')
}
print(json.dumps(body))
conn.request("POST", "/Link/to/API", json=body, headers=header)
check_resp(200)
logger.info("Rules changed successfully")
然后使用

script.py ... --cidr-list 10.20.0.0/32,10.30.0.0/32

所有参数都已经是字符串;您不需要对用户强制使用Python字符串文字语法,逗号分隔的字符串足以处理成CIDR地址列表,而无需对用户强制使用Python列表语法。

用户不必了解Python即可使用您的程序。不要让它们提供Python文本作为参数。
--cidr list 10.20.0.0/32,10.30.0.32
,然后
body={…,'cidrList':params['--ciderList'].split(',')}
。向我们展示API代码。这是一个有趣的部分,因为它似乎不接受正确的JSON。还向我们显示您发送的头文件。用户不必了解Python就可以使用您的程序。不要让它们提供Python文本作为参数。
--cidr list 10.20.0.0/32,10.30.0.32
,然后
body={…,'cidrList':params['--ciderList'].split(',')}
。向我们展示API代码。这是一个有趣的部分,因为它似乎不接受正确的JSON。同时向我们显示您发送的标题。