pythonshell将json从nodejs发送到python

pythonshell将json从nodejs发送到python,python,node.js,Python,Node.js,在nodejs端,我创建了一个脚本,用于将json发送到python,但我不知道如何检索这些数据并将其转换为字典,在这个问题上有人可以帮助我吗 const schedule = require('node-schedule'); const PythonShell = require('python-shell'); let d = new Date('2018-04-16 10:16:10') var pyshell = new PythonShell('../a.py', {

在nodejs端,我创建了一个脚本,用于将json发送到python,但我不知道如何检索这些数据并将其转换为字典,在这个问题上有人可以帮助我吗

  const schedule = require('node-schedule');
  const PythonShell = require('python-shell');
  let d = new Date('2018-04-16 10:16:10')
  var pyshell = new PythonShell('../a.py', { mode: 'json' });
  let j = schedule.scheduleJob(d, () => {
    pyshell.send({test:1,ff:2});
    pyshell.end(err => {
        if (err) res.send("Error : ", err);
    });
    pyshell.on('message', function(message) {
        // console.log(message)
        // handle message (a line of text from stdout, parsed as JSON)
    });
});
a.py

import sys, json
lines = sys.stdin.readlines()
f = json.loads(lines[0])
print(f)
这给了我一个错误

未定义:1

{'test':1,'ff':2}

SyntaxError:JSON中位置0处的意外标记<


pythonshell的文档中规定,数据流中的每个“消息”都应以“\n”结尾。否则pythonshell无法将其解释为valide消息。在这里,您可以阅读更多内容:

选项:执行选项,包括: 模式:配置数据流经stdin和stdout时如何交换数据。可能的值为:

  • text:每行数据(以“\n”结尾)作为消息发出(默认)
  • json:每行数据(以“\n”结尾)被解析为json并作为消息发出
  • 二进制文件:数据通过标准输出和标准输入按原样传输

  • 同样的问题,你找到解决办法了吗?!您可以使用childprocess spwan运行python脚本查看上面的我的编辑HMM,使用
    stringify
    您只需更改
    'text
    python shell
    模式即可。您切换到
    child\u process
    有什么原因吗?这里有同样的问题,python如何接收数据,在使用json模式时是直接以json格式接收数据,还是必须调用json.loads,以及如何读取数据