Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
/
CSV Transformation with node.js

CSV Transformation with node.js

Nov 13, 2017

The node.js tool can be used to write a script that transforms a CSV file. To get 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.

npm install csv-parse csv-stringify

Create Script

Create the following script in the same directory where you have installed the modules:

// require the filesystem module
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 a new table
var newTable = [];

// loop through the data
for(var i=0; i<table.length; i++) {
  // create a new row
  var newRow = [table[i][0], table[i][1] * 1000];

  // add the row to the new table
  newTable.push(newRow)
}

// generate csv data from the new table
var newData = stringify(newTable, { delimiter: ';' });
// console.log(newData);

// write file
fs.writeFileSync('data_new.csv', newData);

console.log('Yay!');

Run Script

Run the script as following to read the CSV and generate a new CSV:

node script.js

Links

  • The node.js fs module.
, multiple selections available,
For you

Web Development
  • Web Development
    Web Development
     This trigger is hidden
Results will update as you type.
  • A basic Barchart
  • Absolute Positioning
  • Code Editors
  • CSS
  • CSS Animations
  • CSV Transformation with node.js
  • d3.js
  • Desktop Source Compilers
  • Homebrew
  • Hosting Services
  • HTML CSS Javascript Resources
  • Interactive Visualization
  • jQuery
  • Mobile Optimization
  • node.js
  • Page-High Resizable Containers
  • Scrollable Visualization
  • Simple Layout Techniques
  • SVG
  • SVG Visualization
  • Terminal
  • UI Prototyping Tools
  • Version Control with GitHub

{"serverDuration": 19, "requestCorrelationId": "a6c5a17c725b47aa8c97aed8082d9df0"}