我有两个脚本,想从终端调用测试器脚本。
string s = """ output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[{
"base_score": 0.92,
"metric": "ACCURACY"
}]"""
脚本#1(script1.py)-由一个解析字符串的函数组成
def parser_score(s):
dict_txt = re.search('\[([^]]+)', s).group(1).strip()
data = json.loads(dict_txt)
return data
脚本#2(script2.py)-调用脚本#1并将结果保存在json文件中
import sys
from parser_for_score_v3 import parser_score
s = sys.argv[1]
print(s)
result = parser_score(s)
# save dict to a json file
with open('result.json', 'w') as fp:
json.dump(result, fp)
我从终端调用第二个脚本-
abc $ python script2.py """output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[{
"base_score": 0.92,
"metric": "ACCURACY"
}]"""
我收到一个错误:AttributeError:'NoneType'对象没有属性'group' -bash:base_score ::找不到命令
先谢谢了。
答案 0 :(得分:1)
精确的文字多行字符串
output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[{
"base_score": 0.92,
"metric": "ACCURACY"
}]
...只需要单引号将其传递到Shell命令行即可。因此:
python script2.py 'output/directory/366d595b-23b2-435d-8dc6-698b3d0844b9/result.csv scores=[{
"base_score": 0.92,
"metric": "ACCURACY"
}]
'
...将在sys.argv[1]
中传递该字符串。