You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tom Brasington edited this page Jan 6, 2014
·
1 revision
The csv library provides basic support for manipulating comma-separated value (CSV) files.
Importing:
// First import the csv module:varcsv=require('lib/csv');
Writing CSV files:
// Create a new CSV spreadsheet objectsheet=newcsv.CSV();// Data can be added to the spread sheet row by rowsheet.addRow(['name','age','sex']);sheet.addRow(['Alice','19','F']);sheet.addRow(['Bob','22','M']);sheet.addRow(['Kirby','1','N/A']);// You can set individual cell values directly using the setCell method// This will change Alice's name to Anna:sheet.setCell(1,0,'Anna');// Rows can also be directly addressed with the setRow method// This replaces the last row:sheet.setRow(3,['John','35','M']);// Write the data to a CSV filesheet.writeFile('data.csv');
Reading CSV files:
// Read a spreadsheet from a CSV filesheet=csv.readFile('data.csv');// Prints 4print(sheet.getNumRows());// Prints 'John'print(sheet.getCell(3,0));