我正在一个项目,该项目在其基础级别上解析一个csv文件并对其进行转换,以便仅使用解析文件中的某些列将某些列映射到新文件。包括输入.csv以供参考。
Co Code,Batch ID,File #,Reg Hours,O/T Hours,Reg Earnings,O/T
Earnings,Hours 3 Code,Hours 3 Amount,Hours 3 Code,Hours 3 Amount,Hours 4
Code,Hours 4 Amount,Hours 4 Code,Hours 4 Amount,Earnings 3 Code,Earnings 3
Amount,Earnings 3 Code,Earnings 3 Amount,Earnings 4 Code,Earnings 4
Amount,Earnings 4 Code,Earnings 4 Amount,Earnings 5 Code,Earnings 5
Amount,Earnings 5 Code,Earnings 5 Amount,Temp Dept
83M,1,102,70.00,14.00,,,,,,,,,,,,,,,,,,,,,,,
83M,1,109,92.50,16.50,,,D,5.00,,,,,,,,,,,,,,,,,,,
83M,1,61,70.00,11.50,,,,,,,V,24.00,,,,,,,,,,,,,,,
83M,1,6384,69.00,15.50,,,D,9.50,,,,,,,,,,,,,,,,,,,
83M,1,6568,78.50,29.50,,,D,18.00,,,,,,,,,,,,,,,,,,,
83M,1,6583,84.50,32.50,,,D,10.00,,,,,,,,,,,,,,,,,,,
83M,1,6596,70.50,27.50,,,D,11.50,,,,,,,,,,,,,,,,,,,
83M,1,6627,95.50,23.50,,,D,12.50,,,,,,,,,,,,,,,,,,,
83M,1,6628,83.50,31.50,,,D,12.50,,,,,,,,,,,,,,,,,,,
83M,1,93,45.00,21.50,,,,,,,11,16.00,V,16.00,,,,,,,,,,,,,,,
我发现,当我使用fast-csv提供的转换功能时,会有一栏特别是“小时3量”,它映射到“ D / T小时”,即使返回,也不会返回任何值(尽管有一些)返回转换后的对象之前(请参阅对象输出样本和csv输出样本)。
{ 'Co Code': '83M',
'Batch ID': '1',
'File #': '93',
'Reg Hours': '36.50',
'O/T Hours': '10.00',
'Reg Earnings': '',
'O/T Earnings': '',
'Hours 3 Code': '',
'Hours 3 Amount': '',
'Hours 4 Code': 'H',
'Hours 4 Amount': '8.00',
'Earnings 3 Code': '',
'Earnings 3 Amount': '',
'Earnings 4 Code': '',
'Earnings 4 Amount': '',
'Earnings 5 Code': '',
'Earnings 5 Amount': '',
'Temp Dept': '' }
Co Code,Batch Id,File #,Reg Hours,O/T Hours,D/T Hours,Reg Earnings,OT
Earnings,DT Earnigs,Pay Period Start,Pay Period End,Total
83M,1,6596,30.00,,,,,,12/30/18,1/12/19,
83M,1,6628,66.50,19.00,,,,,12/30/18,1/12/19,
83M,1,6650,20.50,18.50,,,,,12/30/18,1/12/19,
有趣的是,我可以输入一个字符串文字作为'D / T hours的值,它出现在输出的.csv文件中。仔细检查后,我发现如果注释掉转换函数,则输出csv(虽然是原始未转换的形式)将填充“小时3量”值(请参见下文)。我还包括了我的代码。
// Process the file by moving the cells and inserting the start and end date
const processFile = (fileName, file) => {
csv
.fromPath(basePath + file, {
ignoreEmpty: false,
headers: true,
discardUnmappedColumns: true,
oobjectMode: true
})
.transform(obj => {
[startDate, endDate] = fileName;
console.log(obj); // Does not include the Hours 3 Amount property
if (obj && obj['Co Code'] != '') {
return {
'Co Code': obj['Co Code'],
'Batch Id': obj['Batch ID'],
'File #': obj['File #'],
'Reg Hours': obj['Reg Hours'],
'O/T Hours': obj['O/T Hours'],
'D/T Hours': obj['Hours 3 Amount'],
'Reg Earnings': '',
'OT Earnings': '',
'DT Earnigs': '',
'Pay Period Start': startDate,
'Pay Period End': endDate,
Total: ''
};
}
})
.on('error', () => {
return false;
})
.pipe(csv.createWriteStream())
.pipe(
fs.createWriteStream('./working-files/Payroll-files/output/out.csv', {
encoding: 'utf8',
flags: 'a',
headers: true
})
);
};
有人知道为什么tranform函数会在返回转换后的对象之前先去除列吗?
答案 0 :(得分:0)
没关系。我确定我的源.csv有重复的小时数3金额列。如果存在重复,则转换函数显然不会返回该值。