简体   繁体   English

使用 Javascript 和节点 csv-parse 将 csv 文件解析为数组

[英]using Javascript and node csv-parse to parse csv file into an array

I have a project where I have to process an input CSV file and store it into an array that I can add to, then print it out into a CSV file.我有一个项目,我必须处理输入 CSV 文件并将其存储到我可以添加到的数组中,然后将其打印到 CSV 文件中。 I then use the transaction data for the rest of my project so being able to complete this part is vital as testing will be performed with other CSV files.然后,我使用我项目的 rest 的事务数据,因此能够完成这部分至关重要,因为将使用其他 CSV 文件执行测试。

My issue is that whilst using csv-parse if I use console.table(results);我的问题是,在使用 csv-parse 时,如果我使用 console.table(results); it shows the csv objects when I run the.js file in my command terminal so I know its parsing but no matter what I do I cannot get the objects to go into my array variable.当我在命令终端中运行 .js 文件时,它显示了 csv 对象,所以我知道它的解析,但无论我做什么,我都无法将 go 的对象放入我的数组变量中。

console.table(results);控制台.table(结果);

Please can someone give me a hint as to where I've gone wrong:请有人给我一个关于我哪里出错的提示:

var fs = require('fs');
var parse = require('csv-parse');


var transactionValues = []; //Need an array to hold transactions

//constuctor for transactions
function addData (id, accountType, initiatorType, dateTime, transactions) {
  var data = {
    "AccountID" : id,
    "AccountType" : accountType,
    "InitiatorType" : initiatorType,
    "DateTime" : dateTime,
    "TransactionValues" : transactions
  }
  transactionValues.push(data); //should add a new line
}

    var parser = parse({columns: true}, function (err, results) {
console.table(results);
addData(results.index[0].AccountID, results.index[0].AccountType, results.index[0].InitiatorType, results.index[0].DateTime, results.index[0].TransactionValue, 0);
}); //attempted to save the objects into the array but no success

fs.createReadStream(__dirname+'/testData/customer-1234567-ledger.csv').pipe(parser)

console.log(transactionValues); // array is empty

I believe results is already a normal array as it comes back from csv-parse.我相信results已经是一个正常的数组,因为它是从 csv-parse 返回的。 You are trying to access the first element with results.index[0] , but it would just be results[0] .您正在尝试使用results.index[0]访问第一个元素,但它只是results[0] Another issue is that fs.createReadStream(...).pipe(...) is asynchronous.另一个问题是fs.createReadStream(...).pipe(...)是异步的。 That means your console.log will run before it is done parsing.这意味着您的console.log将在完成解析之前运行。 You would need to put any code that has to run after parsing in the callback of your parse function.您需要将解析后必须运行的任何代码放入parse function 的回调中。 Something like this:像这样的东西:

var parser = parse({columns: true}, function (err, results) {
  console.table(results);
  for (const row of results) { //loop through each object parsed from the csv
    addData(row.AccountID, row.AccountType, row.InitiatorType, row.DateTime, row.TransactionValue, 0);
  }
  console.log(transactionValues); // this should be populated properly

  /* Do anything that needs to use transactionValues here */
});

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

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