www.webdeveloper.com
Results 1 to 3 of 3

Thread: Parsing through CSV to get specific name/value pairs

  1. #1
    Join Date
    Dec 2003
    Posts
    54

    Parsing through CSV to get specific name/value pairs

    Hello,

    I have a task that could be made much easier if I built a simple tool to parse through a CSV data pasted in a text area and it spits out specific the parts the name/value pairs I need. I do not have the luxury of a database and the data is a bit sensitive or else I would not be needing your fantastic assistance and I would have churned out some server-side magic.

    Anyway, what is tripping me up is the necessity for regular expressions (and my lack of experience in this area in general), which can drive a man to drink. I can work with the code to get the specific parts I need and manipulate what is spit out to my specifications but its the starting point that I need help with.

    I pasted a sample section of the data I need to parse through. I am pretty sure I can work with the data once it gets split into an array that can be searched through.

    I appreciate any assistance on this matter.

    Code:
    col1
    col2,col3,col4,col5,col6
    "head1",
    "cat1",1.58000%,000.10,2,724.11,11.64,
    "cat2",2.95000%,000.10,1,150.00,4.53,
    "cat3",1.73000%,000.10,3,1695.42,29.62,
    "cat4",1.05000%,000.15,5,589.79,6.95,
    "head2",
    "cat5",1.54000%,000.10,4,1400.08,21.96,
    "cat6",1.65000%,000.10,5,3287.84,54.74,
    "cat7",2.20000%,000.10,2,5030.40,110.86,
    "cat8",2.30000%,000.10,1,202.48,4.75,
    "cat9",1.20000%,000.00,1,834.46,10.01,
    "cat10",0.95000%,000.20,3,83.86,1.39,
    "cat11",1.75000%,000.20,3,729.18,13.36,
    "total",,,,,269.81,

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,350
    You're text file does not look like a true CSV file.

    It appears you define col1-col6, but you have 7 cols of text without a end "\n"
    or a "Head#" that does not define enough columns of data.

    What kind of information are you trying to parse out of the text file?
    Are the rows ending with a '\n' or a ',' character?
    Can you provide a bit more information about the data you have?

  3. #3
    Join Date
    Apr 2010
    Posts
    213
    As you observe, the operative word here is 'split' . Try this:

    Code:
    function makeCsvArray(text) {
      var rows, i;
      var cols = Array();
      rows = text.split('\n');
      for (i=0; i<rows.length; i++) {
        cols[i] = rows[i].split(',');
      }
      return cols;
    }
    
    var csvArray = makeCsvArray(textField);
    Have fun.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles