Fix Home Needs

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Monday, 14 October 2013

Parsing CSV with Javascript efficiently

Posted on 11:32 by Unknown
CSV parsing is hard. There's not a strict standard, so each implementation is a little different. And when I say CSV, I don't necessarily mean literal "comma-separated values" -- the delimiter could be any character. It's also common to see tabs and pipes as separators.

I work with a lot of delimited text data, and too often I find CSV files that are malformed. Usually quotes are not escaped, or quotes are missing around a field that has special characters like newlines, delimiters, or another quote.

But typical CSV parsers are guilty, too. We can do better.

So here's a way to easily parse CSV with Javascript while keeping the process efficient and error-free as possible, even if you encounter malformed content.

Just use the "Parse" jQuery Plugin

Get the Parse jQuery Plugin on GitHub. It does the hard work for you. If you don't want to have the jQuery dependency, just pull out the "Parse" function from within it. At its core, this plugin doesn't actually need jQuery. (But it's distributed as a jQuery plugin for convenience and publicity.)

The "Parse" jQuery plugin is very easy to use:
results = $.parse(csvString, {
delimiter: ",",
header: true,
dynamicTyping: true
});
The second argument (the config) is optional, but the defaults have been specified here for documentation.

To access a value, say, if you're iterating the resulting rows:

results.rows[i]["Field Name"]

For more instructions on how to use the plugin—it's very easy, I promise—check out the README on GitHub.
Read More
Posted in csv, development, javascript, parsing | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • Writing a Go ("golang") Web App with nginx, FastCGI, MySQL, JSON
    Want to write a web app in Go ("golang") like you write a PHP app? Go is cool since it's kind-of multi-threaded and has some ...
  • How to take FrontRunner from Provo to SLC airport
    I see this question a lot: how do I get from Provo or Orem to the SLC International Airport entirely by train (UTA FrontRunner/Trax)? Here I...
  • Behavior-driven testing in Go with GoConvey (BDD in "golang")
    First: the built-in Go testing tools Few things bring sweeter peace to the soul than making changes to Go code, then: $ go test ... PASS ok ...
  • Why yes, Go/Golang, I still want to read my CSV file!
    UPDATE: This appears to have been fixed and the fix  ships with   Go 1.2 . I like Go (1.1.1), but how disappointing that in order to read ...
  • Installing nginx / PHP / MySQL on Mac OS X Mountain Lion
    ** Update: See a quicker way to do this using Homebrew (this method uses Macports, and it's considerably more difficult). ** ... are yo...
  • Using Vagrant and cross-compiling Go (golang)
    This is mostly a memo-to-self about how to write Go code in my Mac environment, compile it there for a Linux environment, and run it in a pr...
  • Fix the Home and End keys on Mac OS X (Mountain Lion)
    If you use a keyboard that's not designed specifically for Macs, you probably are familiar with the annoying mapping of the Home and End...
  • Install nginx / PHP / MySQL on Mac OS X Mountain Lion with Homebrew
    Last time I wiped my Macbook Pro, I used Macports to install my web development environment . Doing it that way was really hard compared to ...
  • External hard drive backups while you sleep
    On most modern computers, there's an energy saver preference which will shut down your hard disks when the computer is idle or in "...

Categories

  • backup
  • bdd
  • byu
  • chrome
  • cli
  • command line
  • commute
  • compile
  • cross-compile
  • csv
  • development
  • dns
  • domain name
  • fastcgi
  • fcgi
  • go
  • golang
  • homebrew
  • inkscape
  • install
  • ip
  • ip address
  • ipaddress
  • itunes
  • javascript
  • keybinding
  • linux
  • mac
  • mountain lion
  • mysql
  • nginx
  • optimization
  • osx
  • parsing
  • pecl
  • php
  • printing
  • raspberry pi
  • security
  • ssd
  • terminal
  • testing
  • transportation
  • unit tests
  • vagrant

Blog Archive

  • ▼  2013 (16)
    • ►  November (1)
    • ▼  October (1)
      • Parsing CSV with Javascript efficiently
    • ►  September (2)
    • ►  August (2)
    • ►  July (3)
    • ►  June (1)
    • ►  May (1)
    • ►  April (2)
    • ►  March (2)
    • ►  February (1)
  • ►  2012 (8)
    • ►  November (1)
    • ►  October (1)
    • ►  September (6)
Powered by Blogger.

About Me

Unknown
View my complete profile