我正在尝试创建CSV。由于数据很大,我想继续将数据附加到文件中,而不是先将记录存储在一个数组中,然后将所有数据一次性转储到CSV中。所以我编写了以下代码,模拟了我想要做的事情。创建50个随机记录并每秒附加到文件。代码工作正常,但问题是最终的CSV。它看起来如下:
"id","value""value","id"
3226,"3653aab688be4934""value","id"
4070,"9de2be11958fa207""value","id"
2061,"b754b9164146d37f""value","id"
6216,"ac85aa653bfc845d""value","id"
48,"caf5f55c49fde7bf""value","id"
4330,"2c33ae658de7a3eb""value","id"
1997,"34caef7b4ae96edd""value","id"
我无法理解这个原因。我也阅读了相关的SO post,但这也没有帮助。
const json2csv = require('json2csv').parse;
const fs = require('fs');
Promise = require('bluebird');
let plist = [];
let count = 0;
let intvl = null;
let fields = ['id', 'value'];
function start() {
if(count++ > 50) {
Promise.all(plist)
.then(r => {
clearInterval(intvl);
console.log('file created');
process.exit(0);
})
.catch(err => {
console.log(err);
process.exit(-1);
})
}
let data = [{
value: Math.floor(Math.random() * 9999),
id: require('crypto').randomBytes(8).toString('hex')
}];
plist.push(append(json2csv(data)));
}
function append(data) {
return new Promise((resolve, reject) => {
fs.appendFile('./stream.csv', data, (err, resp) => {
if(err) reject(err);
else resolve();
});
});
}
function init() {
fs.stat('./stream.csv', (err, resp) => {
if(err) {
fs.writeFileSync('./stream.csv', json2csv([], {fields}));
}
intvl = setInterval(() => {
start();
}, 1100);
})
}
init();
我错过了什么?最初,代码检查文件是否已存在。如果没有,则仅使用标题创建文件,然后执行常规写入过程。我尝试删除只写标题的部分。它有助于删除顶部的重复标题,但无助于重复每行中的标题。怎么可能被阻止?
答案 0 :(得分:1)
我认为问题是由于代码的多个周期造成的。
每个循环都将fields
标题(“value”,“id”)附加到最后一行,所以:
我们有第一个循环
"id","value""value","id"
3405,"6874eb66f714e717"
第二次循环
"id","value""value","id"
3405,"6874eb66f714e717""value","id" <-- "value","id" added
1436,"c91056b1207598bb"
等等。您应该首次使用fs.writeFileSync('./stream.csv', json2csv([], {fields}));
添加标头,并移除json2csv
返回的额外标头以隔离您的data
。
目前似乎无法使用json2csv
之类的调用将空标头传递给json2csv(data, {})
。
这是一个例子:
const json2csv = require('json2csv').parse;
const fs = require('fs');
Promise = require('bluebird');
let plist = [];
let count = 0;
let intvl = null;
let fields = ['id', 'value'];
function start() {
if(count++ > 50) {
Promise.all(plist)
.then(r => {
clearInterval(intvl);
console.log('file created');
process.exit(0);
})
.catch(err => {
console.log(err);
process.exit(-1);
})
}
let data = [{
id: Math.floor(Math.random() * 9999), //id should be set in this way
value: require('crypto').randomBytes(8).toString('hex')
}];
plist.push(append(json2csv(data).replace('"id","value"',''))); //remove header
}
function append(data) {
return new Promise((resolve, reject) => {
fs.appendFile('./stream.csv', data, (err, resp) => {
if(err) reject(err);
else resolve();
});
});
}
function init() {
fs.stat('./stream.csv', (err, resp) => {
if(err) {
fs.writeFileSync('./stream.csv', json2csv([], {fields}));
}
intvl = setInterval(() => {
start();
}, 1100);
})
}
init();
生成的输出文件stream.csv
:
"id","value"
2462,"7c9197ae6c101f27"
7714,"e1bbfa2dc9adba7a"
2728,"3ff6673cd22bb00b"
8686,"c1f61c138e7b9fdc"
6687,"01d006f74412459a"
7888,"7ccf8e40b9cc4192"
2892,"1672a034573d1be3"
6228,"d8d004148c59134b"
2273,"5028b14b40029d4c"
5114,"1e282fd1c9a84e25"
3636,"c2b7d2250e6fad1e"
8096,"9fb35e54f749417f"
8955,"f2ccc57eab5438a0"
3957,"b323e7addc967d29"