Given this problem CSV file:
<code>"This is a header which doesn't match the CSV"
id,name
1,Australia
2,Austria
3,Belgium
</code>
<code>"This is a header which doesn't match the CSV"
id,name
1,Australia
2,Austria
3,Belgium
</code>
"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)
<code>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);
});
</code>
<code>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);
});
</code>
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)
<code> // columns not set, skipping errors
.pipe(parse({ skip_records_with_error: true }))
</code>
<code> // columns not set, skipping errors
.pipe(parse({ skip_records_with_error: true }))
</code>
// columns not set, skipping errors
.pipe(parse({ skip_records_with_error: true }))
This parses fine but without the headers, output:
<code>┌─────────┬──────┬─────────────┐
│ (index) │ 0 │ 1 │
├─────────┼──────┼─────────────┤
│ 0 │ 'id' │ 'name' │
│ 1 │ '1' │ 'Australia' │
│ 2 │ '2' │ 'Austria' │
│ 3 │ '3' │ 'Belgium' │
└─────────┴──────┴─────────────┘
</code>
<code>┌─────────┬──────┬─────────────┐
│ (index) │ 0 │ 1 │
├─────────┼──────┼─────────────┤
│ 0 │ 'id' │ 'name' │
│ 1 │ '1' │ 'Australia' │
│ 2 │ '2' │ 'Austria' │
│ 3 │ '3' │ 'Belgium' │
└─────────┴──────┴─────────────┘
</code>
┌─────────┬──────┬─────────────┐
│ (index) │ 0 │ 1 │
├─────────┼──────┼─────────────┤
│ 0 │ 'id' │ 'name' │
│ 1 │ '1' │ 'Australia' │
│ 2 │ '2' │ 'Austria' │
│ 3 │ '3' │ 'Belgium' │
└─────────┴──────┴─────────────┘
-
<code> // columns true, skipping errors true.pipe(parse({ columns: true, skip_records_with_error: true }))</code><code> // columns true, skipping errors true .pipe(parse({ columns: true, skip_records_with_error: true })) </code>
// columns true, skipping errors true .pipe(parse({ columns: true, skip_records_with_error: true }))
This produces no output.
4)
<code> // columns true, skipping true, starting from line 2
.pipe(parse({ columns: true, from_line: 2, skip_records_with_error: true }))
</code>
<code> // columns true, skipping true, starting from line 2
.pipe(parse({ columns: true, from_line: 2, skip_records_with_error: true }))
</code>
// 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:
<code>┌─────────┬─────┬───────────┐
│ (index) │ id │ name │
├─────────┼─────┼───────────┤
│ 0 │ '2' │ 'Austria' │
│ 1 │ '3' │ 'Belgium' │
└─────────┴─────┴───────────┘
</code>
<code>┌─────────┬─────┬───────────┐
│ (index) │ id │ name │
├─────────┼─────┼───────────┤
│ 0 │ '2' │ 'Austria' │
│ 1 │ '3' │ 'Belgium' │
└─────────┴─────┴───────────┘
</code>
┌─────────┬─────┬───────────┐
│ (index) │ id │ name │
├─────────┼─────┼───────────┤
│ 0 │ '2' │ 'Austria' │
│ 1 │ '3' │ 'Belgium' │
└─────────┴─────┴───────────┘
<code> // columns, skipping, start from line 1
.pipe(parse({ columns: true, from_line: 1, skip_records_with_error: true }))
</code>
<code> // columns, skipping, start from line 1
.pipe(parse({ columns: true, from_line: 1, skip_records_with_error: true }))
</code>
// 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?