javascript - JavaScript Spawn:如何在 Spawn python 脚本中将变量传递给 FLAG
问题描述
我有一个 python 脚本,它有两个 FLAG--server
和--image
.
目前,在 JavaScript 中,我只能使用 spawn 将固定值分配给 FLAGS。例如:(这确实产生了输出)
var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=./testImage/DSC00917.JPG']);
pyProg.stdout.on('data', function (data) { console.log('This is result ' + data.toString());});
但是,我想分配一个字符串变量并将字符串传递给 FLAG。例如:(这是错误的,它不会产生任何输出)
var imagePath = './testImage/DSC00917.JPG'
var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=imagePath']);
pyProg.stdout.on('data', function (data) { console.log('This is result ' + data.toString());});
我应该如何使它工作?先感谢您!
解决方案
您可以像在 JavaScript 中的任何其他地方一样使用字符串连接。如果你想console.log
打印一个变量,你可以这样做:
console.log('image path is ' + imagePath);
或者如果您使用的是 ES6 字符串插值:
console.log(`image path is ${imagePath}`);
这同样适用于您的代码示例:
var imagePath = './testImage/DSC00917.JPG'
var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=' + imagePath]);
推荐阅读
- corda - 使用全局属性配置数据库
- r - 当有两个值(1个值,1个NA)时,具有连续颜色的r plotly散点图不会显示NA颜色
- python - 在 Cython(.pyx 文件)中使用 pyqt 类
- asp.net-mvc - 身份中的 RedirectToLocal 角色明智重定向
- java - 日期时间方法和解析不起作用
- git - 如何在不丢失文件的情况下撤消 git 提交?
- python - 使用matplotlib创建一个使用步骤连接点的图形?
- c - 确定 read() 和 fork() 系统调用的可能输出
- android - 我收到错误“未注册仪器!必须在注册仪器下运行”
- c++ - 如果在 C++14 或更高版本上,非 const 的成员函数只能是 constexpr