【问题标题】:node.js : reading lines from csv into an arraynode.js:从 csv 读取行到数组中
【发布时间】:2013-08-16 05:41:39
【问题描述】:

我想从一个大的 csv 文件中读取一些行。在 SO 上的快速搜索将我指向“懒惰”模块。这是我的尝试:

items = []
stream = fs.createReadStream src
lazy(stream)
    .lines
    .skip(1)
    .take(5)
    .forEach((line)->
        items.push line.toString())
    .on('end', ->
        console.log items)

但它不打印任何东西。我错过了什么?

【问题讨论】:

    标签: node.js csv stream lazy-sequences


    【解决方案1】:

    'end' 事件似乎没有沿着链发出。仅限'data' and 'pipe' are

    基于the definition of .join()'pipe' 似乎是lazy 使用的。

    # ...
        .forEach((line)->
            items.push line.toString())
        .on('pipe', () ->
            console.log items)
    

    您也可以只使用.join() 来保留lazy 自己的API:

    # ...
        .forEach((line)->
            items.push line.toString())
        .join(() ->
            console.log items)
    

    旁注:您不一定需要自己收集items。你也可以使用.map():

    lazy(stream)
        .lines
        .skip(1)
        .take(5)
        .map(item -> item.toString())
        .join((items) -> console.log items)
    

    或者可能只是:

    # ...
        .map(String)
        .join(console.log);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2016-11-04
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      相关资源
      最近更新 更多