javascript - 将数据从节点发送到python并打印
问题描述
我一直在尝试在节点和 python 之间进行通信,我想将对象数组发送到 python,并在 python 中打印,但我的代码不起作用。
content=[
{
"username": "admin",
"first_name": "",
"last_name": "",
"roles": "system_admin system_user",
"locale": "en",
"delete_at": 0,
"update_at": 1511335509393,
"create_at": 1511335500662,
"auth_service": "",
"email": "adminuser@cognizant.com",
"auth_data": "",
"position": "",
"nickname": "",
"id": "pbjds5wmsp8cxr993nmc6ozodh"
},
{
"username": "chatops",
"first_name": "",
"last_name": "",
"roles": "system_user",
"locale": "en",
"delete_at": 0,
"update_at": 1511335743479,
"create_at": 1511335743393,
"auth_service": "",
"email": "chatops@cognizant.com",
"auth_data": "",
"position": "",
"nickname": "",
"id": "akxdddp5p7fjirxq7whhntq1nr"
}]
JavaScript 代码:
const express=require('express')
const app=express()
let p = require('python-shell');
app.get('/send',(req,res)=>{
var options = {
args:
[
content
]
}
p.PythonShell.run('hello.py', options, function (err, results) {
console.log(results.toString())
});
})
app.listen('5000')
Python脚本:
import sys
import json
details=sys.argv[1]
print (details)
解决方案
由于我们无法将对象传递给命令行,因此python-shell
正在调用 .toString()
内容对象的方法。制作它object Object
。
解决方案:options
将您的对象
更改为:
var options = {
args:
[
JSON.stringify(content)
]
}
在 python 文件中:
parsed = json.loads(sys.argv[1])
print (json.dumps(parsed))
推荐阅读
- javascript - 通过 Javascript 插入的 attr 换行的 CSS 伪元素内容值
- amp-html - 放大器多页表单按钮禁用,直到选定的字段完成
- google-bigquery - Google BigQuery 加入问题查询失败错误:ON 子句
- regex - 如何在记事本++中使用正则表达式替换字符串的同时保留其中的一部分
- reactjs - 访问用户个人资料页面中的道具并更新每次表单更改的值
- r - Rmd Flexdashboard 中的 ShinyAlert
- javascript - 如何打开本地 excel 工作表以在 javascript 中单击按钮进行编辑?
- python - 根据日期时间索引的日期元素索引数据框
- sqlite - 当我输入命令时,构建 gradle 崩溃了:compile project(':react-native-sqlite-storage')
- php - 嗨,我想在 Laravel 的 leftJoin 或 LIMIT 1 中获得 MAX 值,我该怎么做?