繁体   English   中英

问题从 nodejs 产生的 python 进程读取输出

[英]Issue reading output from python process spawned by nodejs

在从 node.js 中生成的 python 进程读取输出时,我遇到了字符编码问题。

这是启动生成进程的代码,结果是一个 JSON 对象。

const python = spawn('python', ['pvm.py'])
python.stdin.write(JSON.stringify(results));
python.stdin.end();

以及生成的进程打印数据时的回调

python.stdout.on('data', async function (result) {
    let pvm = result.toString()
    //other stuff
}

python 文件只是打印一个字典,其中包含通过处理来自 stdin 的 JSON 生成的字符的键和值。 直接运行 python 文件时,我收到 'ʊ': [2, []] 作为输出的一部分。 生成的进程中的缓冲区在转换为字符串时有 'ʊ': [1, [' ', ' ']] 代替,我认为这是由将打印的 python 文本放入缓冲区时出错引起的。

有没有人对为什么会发生这种情况以及如何解决它有任何建议?

将来遇到此问题的任何人如果使用 += 将特殊字符添加到列表中,请改用 append。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM