【发布时间】:2021-03-07 13:34:27
【问题描述】:
这是一个打字稿 任何人都可以提供以下帮助:
- 我从 CSV 文件中读取数据
- 在飞行中转换此数据(删除一些额外的列)
- 然后我希望更新的 csv 在流中返回到代码中的变量。 Console.log(updatedCsv) // 在流中 - 显示我需要的内容 但! 当我尝试将其推送到数组中时,没有任何反应,然后变量(我从流中推送数据)被认为是未定义的:
从“fs”导入*作为fs; 从“csv”导入 * 作为 csv;
udateCsv(){
fs.createReadStream('allure-report/data/suites.csv')
.pipe(csv.parse({ delimiter: ',', columns: true }))
.pipe(csv.transform((input) => {
console.log(input) // <----- it shows in console data I needed
/* like this:
{
Status: 'passed',
'Start Time': 'Wed Nov 11 17:37:33 EET 2020',
'Stop Time': 'Wed Nov 11 17:37:33 EET 2020',
'Duration in ms': '1',
'Parent Suite': '',
Suite: 'The Internet Guinea Pig Website: As a user, I can log into the secure area',
'Sub Suite': '',
'Test Class': 'The Internet Guinea Pig Website: As a user, I can log into the secure area',
'Test Method': 'Hook',
Name: 'Hook',
Description: ''
}
*/
skipHeaders.forEach((header) => delete input[header]);
this.rowsArray = input // NOTHING HAPPENS, rowsArray: string[] = new Array(); input - I don't know what is the type or if I use push. I can't get this data out of pipe
return input;
}))
.pipe(csv.stringify({ header: true }))
.pipe(fs.createWriteStream( this.path))
还有 作为一种解决方法,我想阅读新生成的 csv,但它也是不安全的,看起来我需要使用 Promise。我尝试了一些来自互联网的示例,但失败了。 请帮忙
【问题讨论】:
标签: javascript typescript csv parsing