Code Monkey home page Code Monkey logo

Comments (5)

kulwantPrime avatar kulwantPrime commented on August 20, 2024

Hi,
I was trying to parse the csv file which is quite big file and i wanted to parse the file upto paritcular point where i get the data i wanted and wanted the parsing to stop after that but there is no option of stopping the parsing and if there is option to stop parsing how to do that .
actually there was one logic i developed to stop the parsing to assume the content after the data wanted to take as comment but i was not able to use that option.

from fast-csv.

summer4096 avatar summer4096 commented on August 20, 2024

http://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

fs.createReadStream('sample.csv', {start: 0, end: 1000}) //read the first 1000 bytes
  .pipe( csv({headers: true}) )
  .on('data', function(row){
    console.log('Got row', row);
  })

If you want to read exactly half of a file, you can use fs.stat to get the size, then divide it by two.

Note that you may get an invalid row at the end if a row gets cut off in the middle.

from fast-csv.

kulwantPrime avatar kulwantPrime commented on August 20, 2024

Thanks for the reply ,
but i wanted to parse like first 20 or may be first 30 rows from csv file .so, how i can get the byte size for these 20 or 30 rows of file.

from fast-csv.

summer4096 avatar summer4096 commented on August 20, 2024

You can't get the byte size for a certain number of rows unless you already know what's in the file, and in order to do that you (usually) have to read the file.

Here's an example which will read 20 rows and then stop.

var fs = require('fs');
var csv = require('fast-csv');

var fileStream = fs.createReadStream('sample.csv', 'utf8');
var csvStream = csv({headers: true});

fileStream.pipe(csvStream);

var rows = [];
var onData = function(row){
  rows.push(row);
  if (rows.length == 20) {
    csvStream.emit('donereading'); //custom event for convenience
  }
};
csvStream.on('data', onData);
csvStream.on('donereading', function(){
  fileStream.close();
  csvStream.removeListener('data', onData);
  console.log('got 20 rows', rows);
});

from fast-csv.

kulwantPrime avatar kulwantPrime commented on August 20, 2024

Thanks for Reply.
Problem is solved.

from fast-csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.