我正在尝试将一个MD数组写入csv文件,但我从第2行开始得到一个领先的逗号。 这是方法:
writeDataFile(dataPath, data, COLUMNS) {
const FS = require('fs');
let finalData = [];
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < COLUMNS; j++) {
if (j < COLUMNS - 1) {
finalData.push(data[i][j]);
} else {
finalData.push(data[i][j]+ '\n');
}
}
finalData[i] = finalData[i].replace(/(^,)|(,$)/g, "");
let fileHandle = FS.writeFileSync(dataPath, finalData, 'utf8');
}
}
以下是示例输出:
User,One,101
,User,Two,102
,User,Three,103
这是MD阵列:
[[User][One][101]]
[[User][Two][102]]
[[User][Three][103]]
答案 0 :(得分:0)
以下是我的表现:
writeDataFile(dataPath, data, COLUMNS) {
const FS = require('fs');
for (let i = 0; i < data.length; i++) {
let line = "";
for (let j = 0; j < COLUMNS; j++) {
if (j < COLUMNS - 1) {
line = line + data[i][j] + ',';
} else {
line = line + data[i][j] + '\n';
}
}
let fileHandle = FS.appendFileSync(dataPath, line, 'utf8');
}
}