Parsing CSV Files With NodeJS
I’ve been doing a lot with nodejs lately and thought I’d share a small spike I just did for fun to explore some of the features more in depth and gain a better understanding of some of the IO features I haven’t had a reason to use yet. Keep in mind I did this as a spike completed within an hour so it is quite sloppy!
I kind of envisioned a really simple API with an interesting catch. A function that takes a csv filename as the first parameter and a function as the second parameter to handle each record read in with each record containing the header names as keys and the values to match them. Something like this
Implementation was quite simple using fs.createReadStream.
Here’s the output that gets printed to the commandline:
An earthquake of magnitude 2.3 in Southern Alaska An earthquake of magnitude 1.0 in Northern California An earthquake of magnitude 3.3 in Southern Alaska An earthquake of magnitude 1.1 in Southern California An earthquake of magnitude 1.2 in Central California An earthquake of magnitude 1.1 in Central Alaska An earthquake of magnitude 5.6 in Bio-Bio, Chile An earthquake of magnitude 3.5 in Baja California, Mexico An earthquake of magnitude 2.4 in Alaska Peninsula An earthquake of magnitude 1.2 in Northern California An earthquake of magnitude 5.5 in New Ireland region, Papua New Guinea An earthquake of magnitude 2.0 in Southern California An earthquake of magnitude 1.5 in Southern Alaska An earthquake of magnitude 1.2 in Central Alaska
This spike uses a csv file I got off of data.gov containing earthquakes for the past 7 days. Fun stuff. ![]()
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!














August 6th, 2010 at 8:37 pm
Sweet! How would you include a sorting function. Mayeb some type of mapping. How would you go about extending it for that without changing the code?