【发布时间】:2017-06-19 08:55:55
【问题描述】:
我正在尝试生成一个子进程,该子进程使用从文件中获取的 json 参数调用命令。但是,子进程使用的 json 参数似乎会产生错误,因为 json 有一个 u 前缀表示它是 unicode。
我不确定如何解决这个问题,因为我认为我在形成命令时已经将 json 转换为字符串
import sys
import json
from subprocess import Popen, PIPE
json_file = sys.argv[1]
# Read the provided policy file
with open(json_file, 'r') as f:
values= json.load(f)
for value in values:
command = ('''value definition create --name {} --rules '{}'
''').format(value['name'], value['definition'])
p = Popen(command, stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = p.communicate()
print(stdout)
print(stderr)
std 错误输出如下:
error: unrecognized arguments: values uif: {ufield: utype, ...
这是json文件:
[
{
"name":"testValue",
"definition":{
"if":{
"field":"type",
"in":[
"TestValue"
]
},
"then":{
"effect":"Deny"
}
}
}
]
【问题讨论】:
-
请说明
values和command是什么。
标签: python python-2.7 subprocess