Keyang / node-csvtojson

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

Reading header of CSV-file #146

Closed tom-heyvaert closed 7 years ago

tom-heyvaert commented 7 years ago

I am looking for the fastest way to read the header of a CSV-file without reading the rest of the file.

tom-heyvaert commented 7 years ago

After some trial and error I came up with following solution.

        var preview = [];

        var stream = fs.createReadStream('./uploads/' + file)
            .pipe(
                csv({ 
                    delimiter: 'auto',
                    trim: true
                })
            )
            .on('json',function(jsonObj, index) {
                if(index <= amount) {
                    preview.push(jsonObj);
                } else {
                    stream.end();
                }
            })
            .on('done', function(err) {
                callback(preview);
            })

Parses X amount of CSV-rows and returns them parsed as json in the callback.

Keyang commented 7 years ago

you can use noHeader:true with 'csv' event. the first call back is the header row

csv({noHeader:true}).on('csv',function(row,idx){})

~Keyang

On 14 March 2017 at 15:32, tom-heyvaert notifications@github.com wrote:

Closed #146 https://github.com/Keyang/node-csvtojson/issues/146.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Keyang/node-csvtojson/issues/146#event-999264569, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCs95Pqix0R2uRaBQr2xM4P3dYOyeRGks5rlrMCgaJpZM4McPH9 .