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.
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.
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.
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.
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.
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
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With