My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
CSV  
Convert CSV data into arrays
Updated Feb 4, 2010 by root.n...@gmail.com

Convert CSV data into arrays

<script src="jquery.csv.js"></script>
<script>
// Convert CSV data into array of arrays
jQuery.csv()("1,2,3\n4,5,6\n7,8,9\n"); // = [ [1,2,3], [4,5,6], [7,8,9] ]

// It handles quotes
jQuery.csv()('a,"b,c",d'); // = [ ['a', 'b,c', 'd'] ]

// You can use any delimiter, e.g. tab
jQuery.csv("\t")("a\tb\nc\td\n"); // = [ ['a','b'], ['c','d'] ]

// Quick usage with AJAX:
jQuery.get(csvfile, function(data) { array = jQuery.csv()(data); });

// Using across multiple files
var getTSV = jQuery.csv("\t");
jQuery.get(csvfile1, function(data) { array1 = getTSV(data); });
jQuery.get(csvfile2, function(data) { array2 = getTSV(data); });
...
</script>

Parameters

jQuery.csv(delim, quote, linedelim)

delim column delimiter Comma by default. Use "\t" for tab delimited files. Use "\t,\|" to use any of TAB, comma or pipe
quote quote character Double quote by default. Use "'\"" for either single or double quote
linedelim row delimiter "\r\n" by default. Use "\r\n\|" to specify any of "\r", "\n" or pipe

Comment by tetonp...@gmail.com, Nov 17, 2008

07,Pyle St And Priscilla St,Salisbury,38.374166,-75.581424 06,229 Broad St,Salisbury,38.367896,-75.596564 04,1010 N Salisbury Bv,Salisbury,38.375122,-75.59233 03,751 S Salisbury Bv,Salisbury,38.356721,-75.600218 02,521 E Isabella St,Salisbury,38.369112,-75.59215 01,507 Emory Ct,Salisbury,38.360348,-75.571045 01,309 E Mill Pond Ln,Salisbury,38.363354,-75.594828 01,1523 Edgemore Av,Salisbury,38.382273,-75.587135 01,1305 S Salisbury Bv,Salisbury,38.342888,-75.604498

Comment by mbv40192...@gmail.com, May 24, 2011

Incredible!!!

thanks a lot for the code, works fine for me!

Comment by petras.kuprys@cmegroup.com, Jun 3, 2011

I'm having trouble getting it to work. I'm trying to create an array from a .csv file. Suggestions?

Comment by KiyonoAr...@gmail.com, Aug 16, 2011

I'm also trying to make an array from a csv file. I got it to work perfectly on IE but not Firefox. I get a "syntax error" line 1 col 1. Anyone know what i'm doing wrong?

Comment by Banderse...@gmail.com, Sep 23, 2011

No idea how i get this to work .

where do "insert" my csv file. and what is the array called ?

Comment by arunthe...@gmail.com, Nov 17, 2011

Great piece of code. Saved a day for me :)


Sign in to add a comment
Powered by Google Project Hosting