nodejs 调用 python

May 28, 2021

在一些数据分析中,nodejs 需要调用 python 脚本,来获取数据。

例如:使用 python 分词。

核心方法

var exec = require("child_process").exec

exec("python3 main.py", (err, stdout, stderr) => {
  console.log(stdout)
})

包装成 promise 使用

var exec = require("child_process").exec

function getJieba() {
  return new Promise((resolve, reject) => {
    exec("python3 main.py", (err, stdout, stderr) => {
      resolve(stdout)
    })
  })
}

getJieba().then(data => {
  console.log(data)
})

Profile picture

Written by Vance who lives and works in Shenzhen, China, and is working hard to improve. You should follow them on csdn