Welcome folks today in this blog post we will be parsing and reading
contents of csv
file using a-csv
library in javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new node.js
project using the below command
npm init -y
npm i a-csv
After that you need to make a new index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
var CSV = require("a-csv"); var file = "users.csv"; var options = { delimiter: ",", charset: "win1250", }; CSV.parse(file, options, function (err, row, next) { if (err) { return console.log(err); } if (row !== null) { console.log(row); return next(); } console.log("finish"); }); |
As you can see we are importing the library
at the top and then we are loading the input csv
file which is users.csv
file and then we are providing the delimiter
which is comma and then we are using the parse()
method to provide the input
file and then we are providing the options
object and then we are also having the callback
function in which we will be reading the contents
of the file and showing it inside the command line.