Keyang / node-csvtojson

Blazing fast and Comprehensive CSV Parser for Node.JS / Browser / Command Line.
MIT License
2.02k stars 270 forks source link

Ignore lines before the header #345

Open felipecespedes opened 5 years ago

felipecespedes commented 5 years ago

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

silesky commented 4 years ago

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;
    })
revoorunischal commented 4 years ago

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

zirho commented 2 years ago

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

lines.length 60
lines.length 1