How to use Papa Parse for javascript csv parsing

5.8k views Asked by At

I looked at their API but didn't have any success with it.

I'm trying to parse some csv files that are being sent to the client when he enters the server.

I tried this code:

// Parse local CSV file
Papa.parse("data/premier league/14-15s.csv", {
    complete: function(results) {
        console.log("Finished:", results.data);
    }
});

which didn't work. It gives the following output:

Finished: [Array[1]]0: Array[1]0: "data/premier league/14-15s.csv"length: 1__proto__: Array[0]length: 1__proto__: Array[0]concat: function concat() { [native code] }constructor: function Array() { [native code] }entries: function entries() { [native code] }every: function every() { [native code] }filter: function filter() { [native code] }forEach: function forEach() { [native code] }indexOf: function indexOf() { [native code] }join: function join() { [native code] }keys: function keys() { [native code] }lastIndexOf: function lastIndexOf() { [native code] }length: 0map: function map() { [native code] }pop: function pop() { [native code] }push: function push() { [native code] }reduce: function reduce() { [native code] }reduceRight: function reduceRight() { [native code] }reverse: function reverse() { [native code] }shift: function shift() { [native code] }slice: function slice() { [native code] }some: function some() { [native code] }sort: function sort() { [native code] }splice: function splice() { [native code] }toLocaleString: function toLocaleString() { [native code] }toString: function toString() { [native code] }unshift: function unshift() { [native code] }Symbol(Symbol.iterator): function ArrayValues() { [native code] }Symbol(Symbol.unscopables): Object__proto__: Object

where is the csv??

2

There are 2 answers

0
AudioBubble On BEST ANSWER

Although this question was posted 5 months ago, I believe I've been having a similar problem, and I just solved it, so I thought I'd share what I THINK is the solution, in case other beginners, like myself stumble across in search of an answer.

It looks to me like you're trying to have papa parse parse through an csv file on your machine through a path. I believe that this would mean you're parsing a remote file, which on the papa parse website looks like:

Papa.parse(url, {
    download: true,
    // rest of config...
});

So it looks to me like you're just missing the second argument where download: true. It says in their documentation that url can also be a path - like the one you have. Again, I'm not super confident in my answer because I only started coding about 7 weeks ago, but hopefully that might help someone else who stumbles onto this post in confusion!

7
Matt On

You're parsing a string with the contents data/premier league/14-15s.csv - not a local CSV file. Take another look at the docs. To parse a local CSV file, you must pass in a File object which is obtained from a <input type="file"> element.