我在本地服务器上有一个csv文件。我正在尝试一次获取csvfile中的总行数。但是我无法执行此操作。请帮助。
答案 0 :(得分:0)
正如柏拉图建议的那样,通过库使用CSV。
FWIW,获取任何文本文件的长度,
var content;
// First I want to read the file
fs.readFile('./local.csv', function read(err, data) {
if (err) {
throw err;
}
content = data;
processFile(); // Or put the next step in a function and invoke it
});
function processFile() {
lines = content.split("\r"); // look up
rowCount = lines.length;
}
答案 1 :(得分:0)
使用此代码段来计算Jason Kim's answer中的行数,它像一个超级按钮一样工作。
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function fileLineCount({ fileLocation }) {
const { stdout } = await exec(`cat ${fileLocation} | wc -l`);
return parseInt(stdout);
};
// Usage
async someFunction() {
const lineCount = await fileLineCount({ fileLocation: 'some/file.json' });
}
P.S。这不仅适用于 csv 的任何类型的文件。