The node.js tool can be used to write a scrip that transforms a CSV file. To ge started make sure you have node.js installed and know how to use the terminal.
Install Modules
You need to install the csv-parse
and csv-stringify
module to be able to turn a CSV file into a data structure and later create a CSV file from that data structure.
Code Block |
---|
|
npm install csv-parse csv-stringify |
Create Script
Create the following script in the same directory where you have installed the modules:
Code Block |
---|
|
// require filesystem modules
var fs = require('fs');
// require csv modules
var parse = require('csv-parse/lib/sync');
var stringify = require('csv-stringify/lib/sync');
// read the file
var data = fs.readFileSync('data.csv');
// convert the data to a table
var table = parse(data, { delimiter: ';' });
// create new table
var newTable = [];
// loop through the data
for(var i=0; i<table.length; i++) {
// create a new rwo
var newRow = [table[i][0], table[i][1] * 1000];
// add the row to the new table
newTable.push(newRow)
}
// generate csv data from new table
var newData = stringify(newTable, { delimiter: ';' });
// console.log(newData);
// write file
fs.writeFileSync('data_new.csv', newData);
console.log('Yay!');
|
Run Script
To read the CSV and generate a new CSV run the script as following: