Are there any CSV parsers floating around on this forum?

I wrote one a long time ago, but it kind of sucked.

You can't search the forum for "CSV". Searching for "parser", I didn't see anything.

Comments

  • PhreaK
    PhreaK Senior Member
    If you're just wanting to read the data into a two (well, technically 3) dimensional array what about iterating over each line with explode(..).
  • PsyenceFact
    PsyenceFact Junior Member
    You could use DuetParseCmdParam from SNAPI.axi to separate a CSV string into individual parameters. Something along the lines of:
    define_function integer ParseCSV(char cCSVString[], char cParameters[][])
    {
    	stack_var integer nNumParameters  // number of parameters found in cCSVString
    
    	nNumParameters = 0
            while (length_array(cCSVString)) {
                nNumParameters++
                cParameters[nParameters] = DuetParseCmdParam(cCSVString)
            }
    	set_length_array(cParameters, nNumParameters)
    
    	return nNumParameters
    }
    
    The total number of variables in the CSV line is returned from the function, and the individual variables are returned in whatever variable was passed as cParameters.

    Andy
  • travis
    travis Junior Member
    https://github.com/laduke/netlinx-csv

    I found some java article about CSV and converted it to netlinx. It's a mess. Seems to mostly work. I've lost interest.
  • DHawthorne
    DHawthorne Junior Member
    It seems to me that is would be as much work adapting a general parser to specific data as writing a new one each time. There is too much variance in how a CSV can store data. Just read it in, line-by-line, and break it into tokens at the commas. What you do with the tokens is the trick, and that is different for every data set.