【问题标题】:send large csv file from NodeJS to python将大型 csv 文件从 NodeJS 发送到 python
【发布时间】:2020-04-13 05:25:16
【问题描述】:

我需要将大型 csv 文件从节点发送到 python。此代码适用于小文件,但不适用于大文件。我也尝试过生成过程。我不明白这是什么问题。如果有人知道正确的代码,请分享

代码:

const express=require('express')
const app=express()    
let p = require('python-shell');
const fs = require('fs');
let filledarray=[]

fs.createReadStream('data.csv')

.pipe(csv())

.on('data', (row) => {



filledarray.push(row)

})

.on('end', () => {

   console.log('CSV file successfully processed');

});

app.get('/send',(req,res)=>{



  var options = {
       args:
       [
           JSON.stringify(filledarray)
       ]
  }
  p.PythonShell.run('hello.py', options, function  (err, results)  {

         if(err) {
           console.error(err)
         }
         else{
            console.log(results)
            res.send(results)
         }

  });

})

app.listen('5000')

错误

 Error: spawn ENAMETOOLONG at ChildProcess.spawn (internal/child_process.js:394:11) at Object.spawn 
 (child_process.js:535:9)

【问题讨论】:

    标签: python node.js


    【解决方案1】:

    您正在向脚本hello.py 发送大量数据作为参数,这就是您收到ENAMETOOLONG 的原因。

    您需要更改 Python 脚本以从 stdin 接收数据,并使用 pyshell.send(data);

    let pyshell = new PythonShell('hello.py', { mode: 'text' });
    
    // sends a message to the Python script via stdin
    pyshell.send('hello');
    

    您可以使用以下 3 种模式之一:

    • 使用文本模式交换文本行
    • 使用json模式交换JSON片段
    • 对其他任何事情使用二进制模式(数据按原样发送和接收)

    在您的特定情况下,您可以使用json 并单独发送每一行。然后在您的 python 脚本中,您可以使用以下内容,取自 python-shell examples

    我不知道任何蟒蛇

    import sys, json
    
    # simple JSON echo script
    for line in sys.stdin:
      print(json.dumps(json.loads(line)))
    
    let pyshell = new PythonShell('hello.py', { mode: 'json' });
    
    fs.createReadStream('data.csv')
    .pipe(csv())
    .on('data', (row) => {
       pyshell.send(row);
    })
    
    

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 2015-11-26
      • 1970-01-01
      • 2014-08-24
      • 2016-12-10
      • 1970-01-01
      • 2020-05-29
      • 2020-04-26
      • 2019-10-21
      相关资源
      最近更新 更多