如何使用 csv-parser 遍历 nodejs 中的行?
How to use csv-parser to loop over rows in nodejs?
我正在尝试编写一个脚本,以帮助我摆脱无聊的工作代码。
所以有时,我会收到一个 .csv 文件,指示我创建 table 个字段,有时一次最多创建 50 个字段! (使用 AL,一种业务中心编程语言。)
我试过写这个;
const fs = require('fs');
const path = require('path');
const csv = require('csv-parser');
const CreatesFile = fs.createWriteStream(path.resolve(__dirname, 'final.txt'), {
flags: 'a' //flags: 'a' preserved old data
})
fs.createReadStream(path.resolve(__dirname, 'AutomateVAR.csv'))
.on('error', () => {
// handle error
})
.pipe(csv())
.on('data', (row) => {
if (row["Var Type"] == "Text" || "Code") {
CreatesFile.write( `field(; "${row["Var Name"]}"; ${row["Var Type"]}[${row["Var Length"]}]) {DataClassification = CustomerContent;} ` + '\r\n');
}
if (row["Var Type"] == "Boolean") {
CreatesFile.write( `field(; "${row["Var Name"]}"; ${row["Var Type"]}) {DataClassification = CustomerContent;} ` + '\r\n');
}
if (row["Var Type"] == "Option")
{
CreatesFile.write( `field(; "${row["Var Name"]}"; ${row["Var Type"]}) { OptionMembers = ${row["Var Value"]};} ` + '\r\n');
}
})
.on('end', () => {
})
并有此测试 .csv 文件;
Var Name,Var Type,Var Value,Var Length
Customer,Code,,20
Employee,Text,,50
Cash Customer,Boolean,,
Car Type,Option,"""One"",""Two""",
我的想法是读取 .csv 文件,遍历每一行,然后写入一个文本文件,然后我将在我正在处理的代码文件中传递该文件。听起来可能很无聊,但可以为我节省大量时间。我是菜鸟 Javascript/Node 开发者所以请原谅我的缺点。
经过运行代码后,text.txt中的输出为;
field(; "Customer"; Code[20]) {DataClassification = CustomerContent;}
field(; "Employee"; Text[50]) {DataClassification = CustomerContent;}
field(; "Cash Customer"; Boolean[]) {DataClassification = CustomerContent;}
field(; "Cash Customer"; Boolean) {DataClassification = CustomerContent;}
field(; "Car Type"; Option[]) {DataClassification = CustomerContent;}
field(; "Car Type"; Option) { OptionMembers = "One","Two";}
这是不正确的。正确的结果应该是;
field(; "Customer"; Code[20]) {DataClassification = CustomerContent;}
field(; "Employee"; Text[50]) {DataClassification = CustomerContent;}
field(; "Cash Customer"; Boolean) {DataClassification = CustomerContent;}
field(; "Car Type"; Option) { OptionMembers = "One","Two";}
非常感谢您的指导。提前致谢!
这一行:
if (row["Var Type"] == "Text" || "Code") {
应该是:
if (row["Var Type"] == "Text" || row["Var Type"] == "Code") {
我正在尝试编写一个脚本,以帮助我摆脱无聊的工作代码。
所以有时,我会收到一个 .csv 文件,指示我创建 table 个字段,有时一次最多创建 50 个字段! (使用 AL,一种业务中心编程语言。)
我试过写这个;
const fs = require('fs');
const path = require('path');
const csv = require('csv-parser');
const CreatesFile = fs.createWriteStream(path.resolve(__dirname, 'final.txt'), {
flags: 'a' //flags: 'a' preserved old data
})
fs.createReadStream(path.resolve(__dirname, 'AutomateVAR.csv'))
.on('error', () => {
// handle error
})
.pipe(csv())
.on('data', (row) => {
if (row["Var Type"] == "Text" || "Code") {
CreatesFile.write( `field(; "${row["Var Name"]}"; ${row["Var Type"]}[${row["Var Length"]}]) {DataClassification = CustomerContent;} ` + '\r\n');
}
if (row["Var Type"] == "Boolean") {
CreatesFile.write( `field(; "${row["Var Name"]}"; ${row["Var Type"]}) {DataClassification = CustomerContent;} ` + '\r\n');
}
if (row["Var Type"] == "Option")
{
CreatesFile.write( `field(; "${row["Var Name"]}"; ${row["Var Type"]}) { OptionMembers = ${row["Var Value"]};} ` + '\r\n');
}
})
.on('end', () => {
})
并有此测试 .csv 文件;
Var Name,Var Type,Var Value,Var Length
Customer,Code,,20
Employee,Text,,50
Cash Customer,Boolean,,
Car Type,Option,"""One"",""Two""",
我的想法是读取 .csv 文件,遍历每一行,然后写入一个文本文件,然后我将在我正在处理的代码文件中传递该文件。听起来可能很无聊,但可以为我节省大量时间。我是菜鸟 Javascript/Node 开发者所以请原谅我的缺点。
经过运行代码后,text.txt中的输出为;
field(; "Customer"; Code[20]) {DataClassification = CustomerContent;}
field(; "Employee"; Text[50]) {DataClassification = CustomerContent;}
field(; "Cash Customer"; Boolean[]) {DataClassification = CustomerContent;}
field(; "Cash Customer"; Boolean) {DataClassification = CustomerContent;}
field(; "Car Type"; Option[]) {DataClassification = CustomerContent;}
field(; "Car Type"; Option) { OptionMembers = "One","Two";}
这是不正确的。正确的结果应该是;
field(; "Customer"; Code[20]) {DataClassification = CustomerContent;}
field(; "Employee"; Text[50]) {DataClassification = CustomerContent;}
field(; "Cash Customer"; Boolean) {DataClassification = CustomerContent;}
field(; "Car Type"; Option) { OptionMembers = "One","Two";}
非常感谢您的指导。提前致谢!
这一行:
if (row["Var Type"] == "Text" || "Code") {
应该是:
if (row["Var Type"] == "Text" || row["Var Type"] == "Code") {