简体   繁体   English

如何在 NodeJS 中解析 CSV 文件

[英]How to parse CSV file in NodeJS

I have a Task.csv file with the following content:我有一个包含以下内容的Task.csv文件:

task1,01/05/2020, 20/05/2020, Active
task2,03/05/2020, 17/05/2020, Active
task3,10/05/2020, 25/05/2020, Active
task4,02/05/2020, 21/05/2020, Active
task5,07/05/2020, 28/05/2020, Active

I want to parse this in JavaScript (NodeJS) and display each line read surrounded by brackets.我想在 JavaScript (NodeJS)中解析它并显示用括号括起来的每一行。 The following is the code I'm using:以下是我正在使用的代码:

function readCsvFile()
{
    var fs = require('fs');
    var textByLine = fs.readFileSync('Tasks.csv').toString().split("\n");   
    var i;<br>

    for (i=0; i<textByLine.length; i++)
    {
        console.log("[" + textByLine[i] + "]");
    }
}

What I expect:我的期望:

[task1,01/05/2020, 20/05/2020, Active]
[task2,03/05/2020, 17/05/2020, Active]
[task3,10/05/2020, 25/05/2020, Active]
[task4,02/05/2020, 21/05/2020, Active]
[task5,07/05/2020, 28/05/2020, Active]

When I run it, the output is:当我运行它时,output 是:

]task1,01/05/2020, 20/05/2020, Active
]task2,03/05/2020, 17/05/2020, Active
]task3,10/05/2020, 25/05/2020, Active
]task4,02/05/2020, 21/05/2020, Active
[task5,07/05/2020, 28/05/2020, Active]

I'm new to JavaScript and NodeJS so any comment would be helpful, thanks.我是 JavaScript 和 NodeJS 的新手,所以任何评论都会有所帮助,谢谢。

Do not read file synchronously.不要同步读取文件。 You can use the built-in module readline to read a file line by line and process each line where you don't have to worry about CLRF.您可以使用内置模块readline逐行读取文件并处理您不必担心 CLRF 的每一行。 Alternatively use a module like fast-csv has many features.或者使用像fast-csv这样的模块,它有很多特性。

fast-csv快速csv

const fs = require('fs');
const path = require('path');
const csv = require('fast-csv');

fs.createReadStream('Tasks.csv'))
    .pipe(csv.parse({ headers: true }))
    .on('error', error => console.error(error))
    .on('data', row => console.log(row))
    .on('end', rowCount => console.log(`Parsed ${rowCount} rows`));

Readline阅读线

   const readline = require('readline');
    const fs = require('fs');

    cost lineReader = readline.createInterface({
      input: fs.createReadStream('Tasks.csv')
    });

    let lineno = 0;
    lineReader.on('line', function (line) {
         lineno++
       // process line here
       // let colValues=  line.split(",") 
    });

   lineReader.on('close', () => {
    console.log('Done reading file');
   });

Please try like this请尝试这样

function readCsvFile()
{
    var fs = require("fs");
    var textByLine = fs.readFileSync("Tasks.csv").toString().split("\n");
    console.log(textByLine);
    const res = textByLine.map((line) => line.split(","));
    console.log(res)
}

output

[["task1","01/05/2020"," 20/05/2020"," Active\r"],
 ["task2","03/05/2020"," 17/05/2020"," Active\r"],
 ["task3","10/05/2020"," 25/05/2020"," Active\r"],
 ["task4","02/05/2020"," 21/05/2020"," Active\r"],
 ["task5","07/05/2020"," 28/05/2020"," Active"]]

To fix your code just use.split() method of Javascript to get the result in desired format.要修复您的代码,只需使用 Javascript 的.split() 方法即可获得所需格式的结果。 I have done a small change in your code and getting response the way you mentioned.我对您的代码做了一些小改动,并按照您提到的方式获得了响应。

function readCsvFile() {
    var fs = require('fs');
    var textByLine = fs.readFileSync('data.csv').toString().split("\n");
    var i;
    for (i = 0; i < textByLine.length-1; i++) {
        console.log(textByLine[i].split("\t"))
    }
}

As much as it can be problematic to import node.js modules for trivial purposes, the parsing of CSV files has enough edge cases and gotchas for it to be better done but one that's well tested.尽管出于微不足道的目的导入 node.js 模块可能会有问题,但 CSV 文件的解析有足够的边缘案例和陷阱,可以更好地完成,但经过充分测试。 The top result on NPM is this: https://www.npmjs.com/package/csv-parser NPM 的最高结果是: https://www.npmjs.com/package/csv-parser

Using a module will also reduce the amount of fiddly and fragile code you'll have to maintain yourself.使用模块还将减少您必须自己维护的繁琐和脆弱的代码数量。

Here's a link to the RFC for CSV files, which is the closest thing I know of to a standard for them: https://tools.ietf.org/html/rfc4180 Hopefully this will give you enough info about the edge cases I mention to appreciate the need to use a module instead.这是 CSV 文件的 RFC 的链接,这是我所知道的最接近他们的标准的东西: https://tools.ietf.org/html/rfc4180希望这会给您提供有关我提到的边缘情况的足够信息意识到需要使用模块来代替。

Thanks for your feedback everyone.感谢大家的反馈。 It solved my problem, I tried @PatrickEvans suggestion first.它解决了我的问题,我首先尝试了@PatrickEvans 的建议。 and it worked!它奏效了! Basically I should be using "\r\n" to split the file into individual lines as I created the CSV file in Windows.基本上,当我在 Windows 中创建 CSV 文件时,我应该使用“\r\n”将文件拆分为单独的行。

So now this is the working code所以现在这是工作代码

function readCsvFile()
{

    var fs = require('fs');
    var textByLine = fs.readFileSync('Tasks.csv').toString().split("\r\n"); 
    var i;

    for (i=0; i<textByLine.length; i++)
    {
        console.log("[" + textByLine[i] + "]");
    }
}

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

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