2016-08-24 35 views
0

我對node.js非常陌生。我有一個腳本可以解析csv並生成所需的輸出文件。現在我想同時從另一個csv獲取一些列數據,並將該值添加到輸出文件。如何在node.js中解析來自兩個csv的數據

腳本:

var csv = require('csv'); 
var fs = require('fs'); 
var progress = require('progress-stream'); 
var date = require('date-and-time'); 

var indexStat = 0; 
var header = []; 

var headerLine = '$lang=en\n\nINSERT_UPDATE Customer;uid;name;address;phoneno' 

var delimeter = ','; 
var semicolon = ';'; 

var inputFile = __dirname+'/project/customer.csv'; 
var outputFile = __dirname+'/project/customer.impex'; 
var inputFileName = 'customer.csv'; 
var outputFileName = 'customer.impex'; 

function generateRecord(json) { 

    var record = semicolon + json.uid + semicolon + json.name + semicolon + json.address; 

    return record; 
} 

var writeStream = fs.createWriteStream(outputFile); 

var parser = csv.parse({ 
delimiter: delimeter 
}, function (err, data) { 
if (err) { 
    console.log(err); 
} 
}); 

var transformer = csv.transform(function (data) { 
var line = ''; 
if (indexStat == 0) { 
    header = data; 
    var line = headerLine; 
} else { 
    var line = generateRecord(generateRecordObject(data)); 
} 
indexStat++; 
writeStream.write(line + '\r\n'); 
}); 

function stringSplitter(dataRow) { 
    var str = dataRow.toString(); 
    return str.split(delimeter); 
} 

function generateRecordObject(dataRow) { 
    var record = {}; 
    dataRow.forEach(function (value, index) { 
    if (header[index] != '') { 
     record[header[index].toLowerCase()] = value; 
    } 
}); 
return record; 
} 

var stat = fs.statSync(inputFile); 
var str = progress({ 
    length: stat.size, 
    time: 100 
}); 

str.on('progress', function (progress) { 
    writeCompletedPercentageForRead(progress.percentage, progress.eta, progress.runtime, progress.speed); 
}); 

function removeLineBreaks(obj) { 
    obj = obj.replace(/\\N/g, ''); 
    obj = obj.replace(/&/g, '&'); 
    return obj; 
} 

function writeCompletedPercentageForRead(p, e, r, s) { 
    process.stdout.clearLine(); 
    process.stdout.cursorTo(0); 
    process.stdout.write(`${inputFileName} read in progress to write ${outputFileName} ... Completed:${parseInt(p, 10)} %, ETA:${e} seconds, Elapsed:${r} seconds, Rate:${parseInt(s/1024, 10)} KBps`); 
}; 

fs.createReadStream(inputFile).pipe(str).pipe(parser).pipe(transformer); 

customer.csv - >

uid,name,address 
1234,manish,bangalore 

上述腳本工作正常,併產生customer.impex文件,如下

INSERT_UPDATE Customer;uid;name;address;phoneno 
;1234;manish;bangalore 

現在我想填充phoneno以及phoneno字段是在另一個csv文件中定義可以說'custome r_phone.csv」。

customer_phone.csv -

uid,phoneno 
1234,98777767467 

我想匹配customer_phone.csv customer.csv的UID列,並得到來自CSV的PHONENO。最後,我想在customer.impex outfile文件中添加phoneno。

我不知道如何在這裏解析兩個csv並生成文件。任何幫助?

回答

0
var Converter = require("csvtojson").Converter; 
var fs = require("fs"); 

var pathToMainCSV = "./customer.csv"; 
var pathToContactsCSV = "./contact.csv"; 

var customerConverter = new Converter({}); 
var contactConverter = new Converter({}); 

var contanierObj = {}; 

function processContacts() { 
    fs.createReadStream(pathToContactsCSV).pipe(contactConverter); 
} 

function createImpexFile() { 
    var headerLine = '$lang=en\n\nINSERT_UPDATE Customer;uid;name;address;phoneno;\n'; 
    Object.keys(contanierObj).forEach(obj => { 
     Object.keys(contanierObj[obj]).forEach(data => { 
      headerLine += contanierObj[obj][data] + ';'; 
     }); 
     headerLine += '\n'; 
    }); 
    fs.writeFile("./new.impex", headerLine, function(err) { 
     if (err) { 
      return console.log(err); 
     } 
     console.log("The file was saved!"); 
    }); 
} 

customerConverter.on("end_parsed", function(jsonArray) { 
    jsonArray.forEach(v => { 
     contanierObj[v.uid] = v; 
    }); 
    processContacts(); 
}); 

contactConverter.on("end_parsed", function(jsonArray) { 
    jsonArray.forEach(v => { 
     contanierObj[v.uid].contact = v.phoneno; 
    }); 
    createImpexFile(); 
}); 

fs.createReadStream(pathToMainCSV).pipe(customerConverter); 

根據您的需要請使用類似上面我已經做了,格式化字符串