Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse records on demand with Node.js?

Is there a Node module that can parse a specific number of records from a CSV file? The use case is to parse a large log file and deliver records to a paging client as requested.

node-csv can't yet do this, and the closest I've found is to read lines one by one, which requires reinventing the CSV parsing wheel, and will break on multi-line records.

But let's lower the bar: how can I parse single-line CSV records one by one with Node.js? Pretty trivial task in most other languages.

like image 374
Dan Dascalescu Avatar asked Oct 25 '13 10:10

Dan Dascalescu


People also ask

How to read data from CSV file using Node JS?

There are other methods in Node.js to extract data from a CSV file. We can also use the readFile () method from fs module instead of creating a readable stream. But it has some limitations. In this article, I will discuss how you can read CSV files using Node.js to convert their data into meaningful information for your application.

What is node Node JS?

Node.js is a JavaScript runtime environment designed with easy web server creation in mind, it also has great libraries (like Express.js) to make it even more convenient. With Node.js, you can quickly accept GET requests with few lines of code.

How do I parse JSON documents in GET requests?

It lets you parse JSON documents in GET requests easily. yourFieldName would be the name of the field in your HTML form, not the id. If you typed ‘Nicholas’ in yourFieldName in the HTML form you submitted, the second console.log line will just print ‘Nicholas’ without any symbols or JSON, so you can insert that straight into your database.

How do I start a node app in Linux?

The first step: If you haven’t already, cd to your project’s directory (wherever you are going to put your Node.js code) and install Express.js using the Node package manager (NPM) at a command prompt (this applies to both Windows and Linux). Also Read: JavaScript Variables – A Bite-Sized Introduction.


2 Answers

As far as I understand, you just want to parse the comma-separated line of values into an array? If so, try this one:

https://npmjs.org/package/csvrow

like image 98
Slavo Avatar answered Sep 21 '22 20:09

Slavo


Parsing a single 'line' (which can also have embedded newlines):

var csv = require('csv'); // node-csv

csv()
  .from.string(SINGLE_LINE_OF_CSV)
  .to.array(function(record) {
    console.log('R', record);
  });

I'm not sure what you mean by 'a specific number of records from a CSV file', or what the issue is exactly. Once you've read the amount you need, just send the response back to the client and you're done.

EDIT: if you want to implement paging, you can use node-csv too:

var csv   = require('csv');
var skip  = 100;
var limit = 10;

csv()
  .from.path('file.csv')
  .on('record', function(row, index) {
    if (index >= skip && index < (skip + limit))
      console.log('R', index);
  });
like image 34
robertklep Avatar answered Sep 21 '22 20:09

robertklep