Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将编码数据从python发送到nodejs_Javascript_Python_Node.js - Fatal编程技术网

Javascript 如何将编码数据从python发送到nodejs

Javascript 如何将编码数据从python发送到nodejs,javascript,python,node.js,Javascript,Python,Node.js,我想将一些字符串数据从Python3发送到nodeJs。字符串是韩语字符,我将其编码为utf8(因为我不知道其他安全发送数据的方法)。当我发送(从python)时,它是ByTestStream,在nodeJs中,我将其作为数组接收。我将此数组转换为字符串。但现在我无法将字符串解码回原始的韩语字符。 以下是我正在使用的一些代码 蟒蛇 input = sys.argv[1] d = bot.get_response(input) data = str(d).encode('utf8') print

我想将一些字符串数据从Python3发送到nodeJs。字符串是韩语字符,我将其编码为utf8(因为我不知道其他安全发送数据的方法)。当我发送(从python)时,它是ByTestStream,在nodeJs中,我将其作为数组接收。我将此数组转换为字符串。但现在我无法将字符串解码回原始的韩语字符。 以下是我正在使用的一些代码

蟒蛇

input = sys.argv[1]
d = bot.get_response(input)
data = str(d).encode('utf8')

print(data)
nodeJs

var utf = require('utf8');
var python = require('python-shell');

var pyt = path.normalize('path/to/my/python.exe'),
        scrp = path.normalize('path/to/my/scriptsFolder/'),


    var options = {
        mode: 'text',
        pythonPath: pyt,
        pythonOptions: ['-u'],
        scriptPath: scrp,
        encoding: 'utf8',
        args: [message]
    };

    python.run('test.py', options, function (err, results) {

        //here I need to decode 'results'
        var originalString = utf.encode(results.toString());// that code is not working for me

    });
我用了几个像utf8这样的LIB来解码,但没有帮助。 有人能告诉我怎么做吗

编辑

我必须编辑一些更多的信息。 我尝试过@smarx方法,但没有成功

我有两个案子:

1.如果我从python以字符串形式发送数据,这里是我在nodeJs中得到的
b'\xec\x95\x88\xeb\x85\x95\xed\x95\x98\xec\x8b\xad\xeb\x8b\x88\xea\xb9\x8c\xec\x9d\xb4\xed\x9a\xa8\xec\xa2\x85\xea\xb3\xa0\xea\xb0\x9d\xeb\x8b\x98\xeb\x8f\x99\xec\x96\x91\xeb\xa7\xa4\xec\xa7\x81\xec\x9e\x85\xeb\x8b\x88\xeb\x8b\xa4


2.如果我对数据进行编码并发送。我得到
�ȳ��Ͻʴϱ�? ��ȿ�� ������! �

我仍然不确定python.run的功能是什么,因为您不会共享该代码,但下面是我的代码版本,它工作正常:

test.py

print("안녕 세상")
app.js

const { exec } = require('child_process');

exec('python3 test.py', function (err, stdout, stderr) {
    console.log(stdout);
});

// Output:
// 안녕 세상
let opt = {mode: 'json', pythonOptions: ['-u'], pythonPath: 'python', encoding: 'utf8'}

let pyshell = new PythonShell('lyric.py', opt);
pyshell.on('message', function (message) {

    console.log(message);  //*** The console msg may still wrong (still ���)
    let json = JSON.stringify(message);
    let fs = require('fs');
    fs.writeFile('myjsonfile.json', json, 'utf8', function () {
    }); //*** The output json file will be correct utf8 output
});

我在使用python shell时遇到了同样的问题

以下是我的解决方案:

.encode('utf-8')后面的字符串是二进制字符串。所以你需要直接在标准输出上打印

在test.py中,它打印一个utf-8 json,其中包括一些中文字符:

sys.stdout.buffer.write(json.dumps({"你好":"世界"}, ensure_ascii=False).encode('utf8'))
print() # print \n at ending to support python-shell in json mode
main.js中

const { exec } = require('child_process');

exec('python3 test.py', function (err, stdout, stderr) {
    console.log(stdout);
});

// Output:
// 안녕 세상
let opt = {mode: 'json', pythonOptions: ['-u'], pythonPath: 'python', encoding: 'utf8'}

let pyshell = new PythonShell('lyric.py', opt);
pyshell.on('message', function (message) {

    console.log(message);  //*** The console msg may still wrong (still ���)
    let json = JSON.stringify(message);
    let fs = require('fs');
    fs.writeFile('myjsonfile.json', json, 'utf8', function () {
    }); //*** The output json file will be correct utf8 output
});
结果: 这表明消息在utf-8中正确接收,因为json输出是正确的。 但是,console.log输出显然失败。
我不知道有没有办法修复console.log输出。(Windows 10)

我在项目中遇到了完全相同的问题,现在我终于找到了答案

我用这些代码解决了我的问题

它可以在windows上工作(macOS和Linux,它们的默认系统将其编码为“utf8”,这样问题就不会发生)

我希望它也能帮助你

#in the python file that your javascript file will call by python-shell module put those code
import sys    
sys.stdout.reconfigure(encoding='utf-8')
我从描述中找到了提示


在节点js中使用python中的数据(字符串)时,我也遇到了同样的问题

我这样解决了这个问题:

如果Windows控制台的代码页不是UTF-8,请尝试将Windows控制台的默认代码页更改为UTF-8。 (在我的例子中,默认代码页是CP949。)

就我而言:

我收到了这样的信息������ 2.���� ��������.

我试过在线编码()


然后我发现我的字符串编码为cp949->解码的utf-8。

python.run是什么?它运行python脚本。我的意思更具体一些。:-)能否共享
python.run
?您编辑了代码,但仍然没有共享
python.run
。请尝试在python代码中打印(d.decode())
。我不知道bot.get\u response在做什么,但根据您的输出,它看起来像是返回了
字节。将问题分为两部分:首先让您的Python代码实际打印您想要的内容,然后确保您的JavaScript代码按原样接收输出。请避免使用回答部分发布其他问题。