简体   繁体   中英

Read a CSV file line-by-line using node.js and ES6/ES7 features

Reading a CSV file line-by-line (ie without loading the whole file into memory) in Python is simple:

import csv
for row in csv.reader(open("file.csv")):
    print(row[0])

Doing the same with node.js involves using something like node-csv , streams and callbacks.

Is it possible to use new ES6/ES7 features like iterators, generators, promises and async functions to iterate over lines of a CSV file in a way that looks more like the Python code?

Ideally I'd like to be able to write something like this:

for (const row of csvOpen('file.csv')) {
  console.log(row[0]);
}

(again, without loading the whole file into memory at once.)

I'm not familiar with node-csv, but it sounds like an iterator using a generator should do it. Just wrap that around any asynchronous callback API:

let dummyReader = {
  testFile: ["row1", "row2", "row3"],
  read: function(cb) {
    return Promise.resolve().then(() => cb(this.testFile.shift())).catch(failed);
  },
  end: function() {
    return !this.testFile.length;
  }
}

let csvOpen = url => {
  let iter = {};
  iter[Symbol.iterator] = function* () {
    while (!dummyReader.end()) {
      yield new Promise(resolve => dummyReader.read(resolve));
    }
  }
  return iter;
};

async function test() {
  // The line you wanted:
  for (let row of csvOpen('file.csv')) {
    console.log(await row);
  }
}

test(); // row1, row2, row3

var failed = e => console.log(e.name +": "+ e.message);

Note that row here is a promise, but close enough.

Paste it in babel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM