admin管理员组文章数量:1401208
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.
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.
Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Oct 25, 2013 at 10:57 Dan DascalescuDan Dascalescu 152k65 gold badges333 silver badges420 bronze badges 3- You should consider writing one :) – SheetJS Commented Oct 25, 2013 at 22:57
- What do you mean by "single-line CSV records"? Because obviously it's not just 1 line, as my reply got downvoted. – Slavo Commented Oct 28, 2013 at 9:32
- @Slavo: no idea who downvoted your answer (wasn't me). By "single-line CSV records", I mean ma-separated tuples without newlines in any of the fields. As in, the regular type of CSV data. – Dan Dascalescu Commented Oct 28, 2013 at 11:56
2 Answers
Reset to default 2As far as I understand, you just want to parse the ma-separated line of values into an array? If so, try this one:
https://npmjs/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);
});
本文标签: javascriptHow can I parse records on demand with NodejsStack Overflow
版权声明:本文标题:javascript - How can I parse records on demand with Node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744287260a2598940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论