我正在node js
采取婴儿步骤。第一项任务,逐行读取分隔文件,并吐出包含字符串“imputed”的行。
#!/usr/bin/env node
var Csv = require("csv"),
Fs = require("fs");
var foods = "/../data/sr26";
Csv()
.from.stream(
Fs.createReadStream(__dirname + foods + "/SRC_CD.txt"),
{"delimiter": "^", "quote": "~"}
)
.to(function(data, count) {
console.log(data);
})
.transform(function(row, index, callback) {
//if (row[1].search(/imputed/) != -1) {
process.nextTick(function() {
callback(
null,
row[1].substr(0, 15) + "… " + row[1].search(/imputed/) + "\n"
)
});
//}
});
打印
Analytical or d… -1
Calculated or i… 14
Value manufactu… -1
Aggregated data… -1
Assumed zero… -1
Calculated from… -1
Calculated by m… -1
Aggregated data… -1
Manufacturer's … -1
Analytical data… -1
但取消注释transform
内的比较会导致根本没有输出。我做错了什么?
答案 0 :(得分:1)
异步使用csv transform method时,必须调用回调。
如果使用null
调用它,则只会跳过该记录。
if (row[1].search(/imputed/) != -1) {
process.nextTick(function() {
callback(
null,
row[1].substr(0, 15) + "… " + row[1].search(/imputed/) + "\n"
)
});
} else {
callback(null, null);
}