我使用了 fast-csv 模块,它只能选择忽略空行,但我需要计算空行的数量。
csv
.fromPath(importFile.path, {
headers: true,
ignoreEmpty: true
})
.on("data", function(data) {
console.log(data)
})
最佳答案
从代码中我看到你已经成功了一半。只需在顶部初始化一个 count
变量,然后在“data”事件的回调中根据您的要求拆分数组中的行并检查该行是否为空。
function getEmptyRows(csv){
return new Promise((resolve, reject) => {
let emptyRows = 0;
csv
.fromPath(importFile.path, {
headers: true,
ignoreEmpty: true
})
.on("data", (data) => {
const lines = data.trim().split(/\s*[\r\n]+\s*/g);
lines.forEach((line) => {
if(line.match(/([^\s,])/)){
count++;
}
});
})
.on('end', () => {
return resolve(count);
});
});
}
您可以通过以下方式使用上述功能-
getEmptyRows(csv).then((count) => {
// do your operation...
});
// or as an async/await
async () => {
const count = await getEmptyRows(csv);
// do your operation...
}
关于javascript - 有什么办法可以统计没有吗? csv 文件中的空白行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56323860/