What is csv?
The csv npm package is a comprehensive library for parsing and handling CSV data in Node.js. It provides a range of tools for reading, writing, transforming, and streaming CSV data, making it a versatile choice for developers working with CSV files in JavaScript.
What are csv's main functionalities?
Parsing CSV
This feature allows you to parse CSV data into arrays or objects. The code sample demonstrates how to parse a simple CSV string.
"use strict";
const parse = require('csv-parse');
const assert = require('assert');
const input = 'a,b,c\nd,e,f';
parse(input, function(err, output){
assert.deepEqual(
output,
[['a', 'b', 'c'], ['d', 'e', 'f']]
);
});
Stringifying CSV
This feature allows you to convert arrays or objects into CSV strings. The code sample shows how to stringify an array of arrays into a CSV string.
"use strict";
const stringify = require('csv-stringify');
const assert = require('assert');
const input = [['a', 'b', 'c'], ['d', 'e', 'f']];
stringify(input, function(err, output){
assert.equal(
output,
'a,b,c\nd,e,f\n'
);
});
Transforming Data
This feature allows you to apply a transformation to the CSV data. The code sample demonstrates how to convert all the values in the CSV to uppercase.
"use strict";
const transform = require('stream-transform');
const assert = require('assert');
const input = [['a', 'b', 'c'], ['d', 'e', 'f']];
const transformer = transform(function(record, callback){
callback(null, record.map(value => value.toUpperCase()));
});
transformer.write(input[0]);
transformer.write(input[1]);
transformer.end();
const output = [];
transformer.on('readable', function(){
let row;
while ((row = transformer.read()) !== null) {
output.push(row);
}
});
transformer.on('end', function(){
assert.deepEqual(
output,
[['A', 'B', 'C'], ['D', 'E', 'F']]
);
});
Streaming API
This feature provides a streaming API for working with large CSV files without loading the entire file into memory. The code sample demonstrates how to read a CSV file as a stream and parse it.
"use strict";
const fs = require('fs');
const parse = require('csv-parse');
const parser = parse({columns: true});
const input = fs.createReadStream('/path/to/input.csv');
input.pipe(parser);
parser.on('readable', function(){
let record;
while ((record = parser.read()) !== null) {
// Work with each record
}
});
parser.on('end', function(){
// Handle end of parsing
});
Other packages similar to csv
papaparse
PapaParse is a robust and powerful CSV parser for JavaScript with a similar feature set to csv. It supports browser and server-side parsing, auto-detection of delimiters, and streaming large files. Compared to csv, PapaParse is known for its ease of use and strong browser-side capabilities.
fast-csv
fast-csv is another popular CSV parsing and formatting library for Node.js. It offers a simple API, flexible parsing options, and support for streams. While csv provides a comprehensive set of tools for various CSV operations, fast-csv focuses on performance and ease of use for common tasks.
_ _ _ _____ _______ __
| \ | | | | / ____|/ ____\ \ / /
| \| | ___ __| | ___ | | | (___ \ \ / /
| . ` |/ _ \ / _` |/ _ \| | \___ \ \ \/ /
| |\ | (_) | (_| | __/| |____ ____) | \ /
|_| \_|\___/ \__,_|\___| \_____|_____/ \/ New BSD License
This project provides CSV parsing and has been tested and used
on large input files. It provides every option you would expect from an
advanced CSV parser and stringifier.
The full documentation of the CSV parser is available here.
Usage
Installation command is npm install csv
.
Quick example
csv()
.from.string(
'#Welcome\n"1","2","3","4"\n"a","b","c","d"',
{comment: '#'} )
.to.array( function(data){
console.log(data)
} );
Advanced example
var fs = require('fs');
var csv = require('csv');
var opts = ;
csv()
.from.path(__dirname+'/sample.in', { delimiter: ',', escape: '"' })
.to.stream(fs.createWriteStream(__dirname+'/sample.out'))
.transform( function(row){
row.unshift(row.pop());
return row;
})
.on('record', function(row,index){
console.log('#'+index+' '+JSON.stringify(row));
})
.on('close', function(count){
console.log('Number of lines: '+count);
})
.on('error', function(error){
console.log(error.message);
});
Migration
This README covers the current version 0.2.x of the node csv
parser. The documentation for the previous version (0.1.0) is
available here.
The functions 'from*' and 'to*' are now rewritten as 'from.' and 'to.'. The 'data'
event is now the 'record' event. The 'data' now receives a stringified version of
the 'record' event.
Development
Tests are executed with mocha. To install it, simple run npm install
, it will install
mocha and its dependencies in your project "node_modules" directory.
To run the tests:
npm test
The tests run against the CoffeeScript source files.
To generate the JavaScript files:
make build
The test suite is run online with Travis against Node.js version 0.6, 0.7, 0.8 and 0.9.
Contributors
Related projects