csv-parser
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
minimist | 1.2.8 | 15.16 kB | MIT | prod |
The "csv-parser" npm package is a convenient and ultra-fast library for parsing CSV files. It converts CSV into JSON at a rate of roughly 90,000 rows per second. CSV-parser is not only prized for its speed but also for its compatibility with the csv-spectrum CSV acid test suite. Whether you're operating in a Node environment or using it in the browser with browserify, this module has you covered.
Using the csv-parser is quite straightforward. Start by installing the package with npm or yarn.
$ npm install csv-parser
or
$ yarn add csv-parser
Once the csv-parser module is installed, you can proceed with implementing it in your JavaScript code as shown below:
// Import the necessary modules
const csv = require('csv-parser')
const fs = require('fs')
// Declare an array to store the parsed data
const results = [];
fs.createReadStream('data.csv') // Create a readable stream to a CSV file
.pipe(csv()) // Pipe the stream to csv-parser
.on('data', (data) => results.push(data)) // On 'data', push the data into the results array
.on('end', () => { // On 'end', log the results
console.log(results);
});
The csv-parser function accepts an object argument which allows you to specify options. For example:
csv({ separator: '\t' }); // to specify a tab character as the separator
For detailed information and advanced usage, you can access the csv-parser documentation on its GitHub repository at https://github.com/mafintosh/csv-parser. The documentation provides a comprehensive overview of the API, including various options you can set and events you can listen to when using csv-parser. Also, you will find useful information on how to use the CLI provided by the module and how to deal with encoding issues and Byte Order Marks.