1

I am using jquery CSV library (https://github.com/evanplaice/jquery-csv/) , to parse and convert a CSV file into an array of objects. This is my code

this.parsedData = $.csv.toObjects(data);

if (this.parsedData.length === 0) {
**console.log('In valid'); /**/ This gets printed 
} else {
    console.log('valid')
}

My CSV file is this:

""__stream_code","project_code","process","due_date","root_table"
"DASH_PLAY_001","DEV","Plate",2013-02-02,"stream"
"DASH_PLAY_001","DEV","DepthAssess",2013-02-03,"stream""

Now, if I remove quotes, it works fine, but with quotes it doesn't. Most of the time, my application is going to handle CSV with values in quotes. Is there any way to fix it?

4

2 回答 2

1

Try with http://papaparse.com/; you can parse it online and it works great.

All you have to do is call through var results = $.parse(csvString);

and it will return the JSON object for you.

于 2014-06-03T00:02:32.833 回答
0

Why is it wrapped in quotes?

I've tested it without the outer quotes on the Basic Usage demo and it seems to parse the data just fine. Are you using at least version 0.71?

This:

""__stream_code","project_code","process","due_date","root_table"
"DASH_PLAY_001","DEV","Plate",2013-02-02,"stream"
"DASH_PLAY_001","DEV","DepthAssess",2013-02-03,"stream""

Double-double quotes cancel each out (as per the spec) so this'll error when it tries to parse the first value.

Should be:

"__stream_code","project_code","process","due_date","root_table"
"DASH_PLAY_001","DEV","Plate",2013-02-02,"stream"
"DASH_PLAY_001","DEV","DepthAssess",2013-02-03,"stream"

If jquery-csv encounters an error during parsing it should log an error to the console indicating the row:column where the error occurred. Is there an error in the console. If not what does console.log(data) output?

As for the data being a mix of quoted/unquoted, the parser shouldn't have any issues consuming it. The data (sans outer quotes) looks perfectly valid.

BTW, I'm not trying to shed blame. If there is something else here that isn't in the code sample you provided that exposes a bug in the parser, I'd like to identify and fix it.

Disclaimer: I'm the author of jquery-csv

于 2016-01-21T14:00:28.343 回答