1

I am trying to read a csv file from 4th row, in my csv file first 3 rows contains some meta data in column1. But the actual number of columns in csv is 6.

eg:

metadata1
metadata2
metadata3
head01,head02,head03,head04,head05,head06
data11,data12,data13,data14,data15,data16
data21,data22,data23,data24,data25,data26

Code I am trying is:

fs.createReadStream('/path/file.csv').pipe(
        parse({ from_line: 4, fromLine: 4 }, function(data, err) {
        console.log(data);
    })
);

I am getting data as undefined. Also If I am removing the args of parse, I am getting error Error: Invalid Record Length: expect 1, got 6 on line 4. Please guide!

1 Answer 1

1

First, Error: Invalid Record Length: expect 1, got 6 on line 4 is because parse function expects the number of columns to be consistent throughout the file. The metadata lines have only 1 column, while the data lines have 6 columns, which is causing the parser to expect only 1 column when reading the 4th line.

So, You were right to skip the first three lines before passing the stream to the parser. ({ from_line: 4 })

But the problem is that you're using the callback API incorrectly.

The signature is parse(data, [options], callback). It takes the CSV in the 1st argument, options in the 2nd argument, and a user callback in the 3rd argument. The callback receives any error thrown by the CSV parser in the first parameter (err), or an array of records in the second argument(data). It returns an array.

So, this is the corrected code.

const fs = require('fs'); 
const { parse } = require('csv-parse');

fs.createReadStream('/path/file.csv').pipe(
  parse({ from_line: 4 }, function(err, data) {
    console.log(data);
  })
);

FYI, this is the example of the Promises usage.

fs.createReadStream('/path/file.csv')
  .pipe(parse({ from_line: 4 }))
  .on('data', (data) => {
    console.log(data);
  })
  .on('error', (err) => {
    console.error(err.message);
  });

I hope this solves your issue.

Sign up to request clarification or add additional context in comments.

2 Comments

This solution works after adding import { parse } from "csv-parse";
yes, you need to import fs and csv-parse. const fs = require('fs'); const { parse } = require('csv-parse');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.