Reading a CSV with Node.js

Here’s a really straightforward way to read a CSV with Node.js & Express. We’ll be using the following modules:

  • fs: “file system” included by default with Node.js, used to interact with files on your computer – or server, in this case
  • path: also included by default with Node, it is used to extract the filename from a path string. Basically, allows our app to work with string paths (“/server/data.csv”)
  • csv-parse: part of the larger CSV node module, this allows us to actually parse the CSV data into arrays.

As usual, here’s a disclaimer: is this the cleanest or best way to parse CSV files? Probably not – but it’ll hopefully give you a starting point.

Let’s say that we have a sample.csv file that looks like this:

Screen Shot 2018-08-02 at 4.00.27 PM

First, let’s get the data. Assuming your file lives in the same directory as your script (if it doesn’t, you’ll need to set your path starting from the root… somehow):

const fs = require("fs");       // access the file system
var path = require('path');     // read paths

console.log("dirname: " + __dirname);     // see your current directory
// create a "/sample.csv" path in the same directory as your script
var filePath = path.join(__dirname, 'sample.csv'); 

// read the file
fs.readFile(filePath, function read(err, data) {
    if (err) {
        console.log(err);
        callback(err);
    } else {
        // parse the file
        parse(data, {delimiter: ','}, function(err, parsedOutput){
            if(err){
                console.log(err);
            } else {
                console.log(parsedOutput);
                // do stuff with your output here
            }
        });
    }
});

Note this line:


parse(data, {delimiter: ','}, function(err, parsedOutput){ ... }));

The bit in {} are options. In this case, we specify that our data is separated by commas (hence Comma Separated Values), but there are other neat ones you can check out in the documentation under “Parse Options”.

Now, parsedOutput will contain your data as a multi-dimensional array, with one array per row, like this:

  [ 
    [ 'number', 'letter', 'planet' ],
    [ '1', 'a', 'mercury' ],
    [ '2', 'b', 'venus' ],
    [ '3', 'c', 'earth' ],
    [ '4', 'd', 'mars' ],
    [ '5', 'e', 'saturn' ],
    [ '6', 'f', 'jupiter' ],
    [ '7', 'g', 'neptune' ] 
  ]

 

Now you can play with your data – slice it up, rearrange it, send it to your front end, convert it to a JSON, and so on!

Hope this helps 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s