如何使用Node.js按需解析记录?

时间:2021-07-05 07:22:21

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文件中的特定数量的记录?用例是解析大型日志文件,并根据请求将记录传送到分页客户端。

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.

node-csv还不能这样做,我发现最接近的是逐行读取行,这需要重新发明CSV解析轮,并且会在多行记录中断开。

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.

但是让我们降低标准:如何使用Node.js逐个解析单行CSV记录?在大多数其他语言中相当简单的任务。

2 个解决方案

#1


1  

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

https://npmjs.org/package/csvrow

#2


0  

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.

我不确定“来自CSV文件的特定数量的记录”是什么意思,或者问题究竟是什么。一旦您阅读了所需的金额,只需将回复发送回客户端即可。

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

编辑:如果你想实现分页,你也可以使用node-csv:

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);
  });

#1


1  

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

https://npmjs.org/package/csvrow

#2


0  

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.

我不确定“来自CSV文件的特定数量的记录”是什么意思,或者问题究竟是什么。一旦您阅读了所需的金额,只需将回复发送回客户端即可。

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

编辑:如果你想实现分页,你也可以使用node-csv:

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);
  });