node.js - 将 CSV 流与节点合并,尝试发出标头一次且仅一次
问题描述
使用 Node.js 和NodeCSV。
我正在循环多个 CSV 流,并尝试将它们合并为一个 CSV,并将标题行作为第一行。我也在过滤以仅获取time
列晚于给定时间的记录,尽管我很确定这部分与我的问题无关。
输入 CSV 具有标题行,其中一些可能不包含预期的列,在这种情况下,它们应该通过而不发出任何记录。
代码(为简洁而修改,请原谅复制/粘贴/复制错误)是这样的:
const csv = require('csv');
let header = true; // first record should emit a header line
const streams = [_array, _of, _read, _streams];
const lastIndex = streams.length - 1;
const sinceTime = Date.parse(someTime);
for (const [i, s] of streams.entries()) {
s
.pipe(csv.parse({columns: true}))
.pipe(csv.transform(function (record) {
const recordTime = Date.parse(record.time);
if (recordTime > sinceTime) {
return record;
}
}))
.pipe(csv.stringify({
columns: ['time', 'col'],
header: header
}))
.pipe(output, {end: i === lastIndex});
// this should only be cleared if the file actually emitted a header, but how do I know!?
header = false;
}
当第一个输入文件不包含time
andcol
列时,就会出现问题,假设它是一个带有X
andY
列的 csv。
在这种情况下,文件不会发出任何记录(如所希望的那样),但结果它也不会发出标题行。但我不知道,并愉快地将header
布尔值设置为false
. 然后我的输出不包含标题行,这不是我想要的。
如果第一个文件包含预期的列time
and col
,那么一切都很好,我从第一个文件中获取标题行。
即使第一个文件没有发出任何记录,我该如何处理这种情况并发出标题行?
更新:
我想到了以下解决方案,它有效,但似乎必须有更好的方法。我尝试在循环之前生成一个带有标题的空 CSV,如下所示:
const csv = require('csv');
const streams = [_array, _of, _read, _streams];
const lastIndex = streams.length - 1;
const sinceTime = Date.parse(someTime);
const columns = ['time', 'col'];
// generate the header line with an empty CSV
csv
.generate({length: 0})
.pipe(csv.parse({columns: true}))
.pipe(csv.stringify({
columns: columns,
header: true
}))
.pipe(output, {end: false});
for (const [i, s] of streams.entries()) {
s
.pipe(csv.parse({columns: true}))
.pipe(csv.transform(function (record) {
const recordTime = Date.parse(record.time);
if (recordTime > sinceTime) {
return record;
}
}))
.pipe(csv.stringify({
columns: columns,
header: false
}))
.pipe(output, {end: i === lastIndex});
}
丑陋,但它正在做我想做的事。有没有更清洁的方法?
解决方案
推荐阅读
- machine-learning - 通过复制人为地增加数据集的大小?
- reactjs - 使用 Heroku 的 React + Express + SQLite 部署在 Get="/" 上给出 404
- javascript - 有没有一种简单的方法可以将 if 语句压缩成一个函数来检查参数?
- c++ - clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 架构 x86_64 的未定义符号:
- javascript - 基于组件状态值的while循环
- java - 为什么 StringBuild 中的字符不会改变?
- database-design - 数据库设计:我应该如何处理电子商务网站的产品删除?
- python - Python3新手需要建议
- java - 将图像壁纸设置为适合屏幕“
- r - asmethod(object) 中的错误:文件 ../core/cholmod_dense.c 中的 cholmod 错误“问题太大”,第 105 行