csvjson.js icon indicating copy to clipboard operation
csvjson.js copied to clipboard

Extra \r characters

Open joevallender opened this issue 13 years ago • 1 comments

Following the thread here from http://stackoverflow.com/questions/3603242/convert-csv-file-to-json-object-datatable/6991791#comment14423359_6991791

The value of the last value in each column (row or header) had an extra \r character at the end

I should point out that I didn't want any type of carriage return / newline character so I am using

/(\r\n|\n|\r)/gm 

for to match those characters. They key to getting a \r off the end being the m option. You may well want to include a config option in your function to toggle this. Or perhaps just try and match a \r specifically

/\r$/m ?

Line 67 csv2json.js:

var item = rowitems[i];

Can just have your chosen pattern match applied:

var item = rowitems[i].replace(/pattern/m,'');

But for

Line 49 csvjson.js:

var csvheaders = splitCSV(csvlines[0], delim);

You'll need to iterate over it. My quick hack (since it only happens to the last row) was this

csvheaders[(csvheaders.length-1)] = csvheaders[(csvheaders.length-1)].replace(/(\r\n|\n|\r)/gm,"");

But you might want to do something less ghastly ;-)

Thanks for the lib!

joevallender avatar Jun 14 '12 14:06 joevallender

Thanks for creating an issue! I'll have a look and see if I can roll in a fix for your case ;)

aaronsnoswell avatar Jun 14 '12 14:06 aaronsnoswell