node-csvtojson icon indicating copy to clipboard operation
node-csvtojson copied to clipboard

Ignore lines before the header

Open felipecespedes opened this issue 6 years ago • 3 comments

I would like to request a new parameter

startOnLine: number

let's say my data look like

----,----,----
----,----,----
field1,field2,field3
val1,val2,val3
val4,val5,val6

So I would use it as

csv({
  startOnLine: 3
}).fromFile(filePath)
.then(...

and then I would get

[
{
  "field1": "val1",
  "field2": "val2",
  "field3": "val3"
},
{
  "field1": "val4",
  "field2": "val5",
  "field3": "val6"
}
]

Awesome project btw! Thanks

felipecespedes avatar Oct 04 '19 15:10 felipecespedes

I achieved this by using the preRawData api.

csv()
    .fromFile(path)
    .preRawData(str => {
      const lines = str.split('\n');
     // header is on second line
      const trimmedStr = lines.filter((_, idx) => idx > 0).join('\n');
      return trimmedStr;
    })

silesky avatar Jan 11 '20 19:01 silesky

preRawData event occurs twice on the file. event that needs to be handled right ?

revoorunischal avatar Aug 03 '20 12:08 revoorunischal

yeah it seems it's being called twice. If I print out lines.length, it looks like this.

lines.length 60
lines.length 1

zirho avatar Sep 27 '22 22:09 zirho