Does someone have an idea on where I could find some javascript code to parse CSV data ?
closed as off topic by Andrew Barber Apr 18 '13 at 1:08Questions on Stack Overflow are expected to relate to programming within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question. |
|||||||||||||||||
|
You can use the CSVToArray() function mentioned in this blog entry.
|
|||||||||||||||||||||
|
Im not sure why I couldn't kirtans ex. to work for me. It seemed to be failing on empty fields or maybe fields with trailing commas... This one seems to handle both. I did not write the parser code, just a wrapper around the parser function to make this work for a file. see Attribution
|
|||
|
I think I can sufficiently beat Kirtan's answer Enter jQuery-CSV It's a jquery plugin designed to work as an end-to-end solution for parsing CSV into Javascript data. It handles every single edge case presented in RFC 4180, as well as some that pop up for Excel/Google Spreadsheed exports (ie mostly involving null values) that the spec is missing. Example:
Update: Oh yeah, I should also probably mention that it's completely configurable.
Update 2: It now works with jQuery on Node.js too. So you have the option of doing either client-side or server-side parsing with the same lib. Disclaimer: I am also the author of jQuery-CSV. |
|||||||||||||||||||||
|
Here's my PEG(.js) grammar that seems to do ok at RFC 4180 (i.e. it handles the examples at http://en.wikipedia.org/wiki/Comma-separated_values):
Try it out at http://jsfiddle.net/knvzk/10 or http://pegjs.majda.cz/online. Download the generated parser at https://gist.github.com/3362830. |
|||||||||||||||||
|
Why not just use .split(',') ? http://www.w3schools.com/jsref/jsref_split.asp
|
|||||||||||||||||||||
|
I have an implementation as part of a spreadsheet project. This code is not yet tested thoroughly, but anyone is welcome to use it. As some of the answers noted though, your implementation can be much simpler if you actually have DSV or TSV file, as they disallow the use of the record and field separators in the values. CSV, on the other hand can actually have commas and newlines inside a field, which breaks most regex and split-based approaches.
|
|||||
|
Here's an extremely simple CSV parser that handles quoted fields with commas, new lines, and escaped double quotation marks. There's no splitting or RegEx. It scans the input string 1-2 characters at a time and builds an array. Test it at http://jsfiddle.net/vHKYH/.
|
|||||||||||||||||
|
csvToArray v1.3 A compact (645 bytes) but compliant function to convert a CSV string into a 2D array, conforming to the RFC4180 standard. http://code.google.com/p/csv-to-array/ Common Usage: jQuery
Common usage: Javascript
Override field separator
Override record separator
Override Skip Header
Override all
|
||||
|