Reputation: 282
Given this problem CSV file:
"This is a header which doesn't match the CSV"
id,name
1,Australia
2,Austria
3,Belgium
how would I parse it in node-csv, skipping that first line, and using the second line as the column headers?
What I've tried:
1)
import fs from 'node:fs';
import { parse } from 'csv-parse';
let rows = [];
fs.createReadStream('./downloaded/bad_data.csv')
// columns option set
.pipe(parse({ columns: true }))
.on("data", function (row) {
rows.push(row);
//console.log(row);
}
).on('end', function () {
console.table(rows);
});
this results in a non-recoverable error because of the quote.
2)
// columns not set, skipping errors
.pipe(parse({ skip_records_with_error: true }))
This parses fine but without the headers, output:
┌─────────┬──────┬─────────────┐
│ (index) │ 0 │ 1 │
├─────────┼──────┼─────────────┤
│ 0 │ 'id' │ 'name' │
│ 1 │ '1' │ 'Australia' │
│ 2 │ '2' │ 'Austria' │
│ 3 │ '3' │ 'Belgium' │
└─────────┴──────┴─────────────┘
// columns true, skipping errors true
.pipe(parse({ columns: true, skip_records_with_error: true }))
This produces no output.
4)
// columns true, skipping true, starting from line 2
.pipe(parse({ columns: true, from_line: 2, skip_records_with_error: true }))
this parses fine but misses the first line of the data:
┌─────────┬─────┬───────────┐
│ (index) │ id │ name │
├─────────┼─────┼───────────┤
│ 0 │ '2' │ 'Austria' │
│ 1 │ '3' │ 'Belgium' │
└─────────┴─────┴───────────┘
// columns, skipping, start from line 1
.pipe(parse({ columns: true, from_line: 1, skip_records_with_error: true }))
This produces no output.
What am I missing and how can I get my columns from line 2 if line 1 contains an error?
Upvotes: 0
Views: 54
Reputation: 120
I have tried your way and its working fine
const fs = require('node:fs');
const { parse } = require('csv-parse');
let rows = [];
fs.createReadStream('temp.csv')
.pipe(parse({ columns: true, from_line: 2, skip_records_with_error: true }))
.on("data", function (row) {
rows.push(row);
}
).on('end', function () {
console.table(rows);
});
Here's the output
┌─────────┬─────┬─────────────┐
│ (index) │ id │ name │
├─────────┼─────┼─────────────┤
│ 0 │ '1' │ 'Australia' │
│ 1 │ '2' │ 'Austria' │
│ 2 │ '3' │ 'Belgium' │
└─────────┴─────┴─────────────┘
Upvotes: 0