删除一行csv或忽略它

时间:2018-07-23 09:11:51

标签: javascript node.js csv

我正在使用fast-csv读取csv文件,但是我的文件格式与fast-csv并不完全一样:

First line with some details (not important)
Second line with header
Third line with data
Fourth line with data
...

以及我的阅读方式:

const csv = require('fast-csv');
const stream = fs.createReadStream('myfile.csv');
const csvStream = csv
    .fromStream(stream,
        {
            headers: true,
            delimiter: ',',
            rowDelimiter: '\n',
            quoteHeaders: false,
            quoteColumns: false
        })
    .on("data", data => {
       // i do something with datas

    })
    .on("error", error => {
        console.log("CSV is invalid !", error);
    })
    .on("end", data => {
        console.log("End of parsing");
        console.log(data);
    });

这里的问题是

  

标题:是

将查找第一行作为标题,这就是为什么我要删除第一行或忽略它的原因。怎么做 ? (我知道我可以在没有第一行的情况下读取文件和写入文件,但是我认为这不是正确的方法...)

版本: 节点v10.0.0和fast-csv 5.6.0


编辑:使用@Anders Carstensen示例进行测试:

const file = 'myfile.csv';
const fs = require("fs");
const stream = fs.createReadStream(file, {
    encoding: 'utf8'
});

stream.on('readable', () => {
    // Read through the stream until we encounter a new line
    let chunk;
    while (null !== (chunk = stream.read(1))) {

        if (chunk == '\n'){
            console.log('line break');
            break;
        }
    }
    console.log('test');


    // CSV parsing
    const csvStream = csv.fromStream(stream,
    {
        renameHeaders: false,
        headers: true,
        delimiter: ',',
        rowDelimiter: '\n',
        quoteHeaders: false,
        quoteColumns: false
    })
    .on("data", data => {
        console.log(data);

    })
    .on("error", error => {
        console.log("CSV invalid !", error);
    })
    .on("end", data => {
        console.log("End of parsing");
        console.log(data);
    });
});

输出:

line break
test
line break
test

Edit2:检查@Anders Carstensen的答案(问题是使用.on而不是.once)

1 个答案:

答案 0 :(得分:2)

首先,您应该仔细检查流中不希望解析器查看的部分。在这里,我阅读直到遇到第一行(\n)为止。

var fs = require('fs'),
    csv = require('fast-csv');

var stream = fs.createReadStream('c:\\temp\\test.csv', {
  encoding: 'utf8'
});

stream.once('readable', function () {
  // Read through the stream until we encounter a new line
  var chunk;
  while (null !== (chunk = stream.read(1))) {
    if (chunk === '\n')
      break;
  }

  // Then do the CSV parsing
  const csvStream = csv
    .fromStream(stream,
      {
        headers: true,
        delimiter: ',',
        rowDelimiter: '\n',
        quoteHeaders: false,
        quoteColumns: false
      })
    .on("data", data => {
      // i do something with datas
      console.log('data', data);
    })
    .on("data-invalid", data => {
      console.log('invalid data', data);
    })
    .on("error", error => {
      console.log("Le fichier CSV est invalide !", error);
    })
    .on("end", data => {
      console.log("End of parsing");
      console.log(data);
    });
});