gpt4 book ai didi

javascript - 将数据从 Node 发送到python并打印

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:59 24 4
gpt4 key购买 nike

我一直在尝试在 Node 和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() 方法。使其成为对象对象

解决方案:将您的 options 对象更改为:

var options = {
args:
[
JSON.stringify(content)

]
}

在 python 文件中:

parsed = json.loads(sys.argv[1])
print (json.dumps(parsed))

关于javascript - 将数据从 Node 发送到python并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59237702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com