0

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??

Eivind
  • 301
  • 6
  • 18

2 Answers2

1

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!

0

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.

Matt
  • 22,721
  • 17
  • 71
  • 112
  • How would I create a File object with that directory? – Eivind Jun 09 '15 at 14:32
  • By doing what I said - create an input element of type file. The user has to select the file(s) in order for the browser to give you access to them. – Matt Jun 09 '15 at 14:33
  • Am I supposed to change the string to ? – Eivind Jun 09 '15 at 14:38
  • No. I recommend reading up on how to use input elements on your web page. Then follow the link I posted about File objects where you can learn how to get those from the DOM. – Matt Jun 09 '15 at 14:39
  • Why do I need to include HTML input elements in order to parse a file? – Eivind Jun 09 '15 at 14:40
  • I don't have time for reading up right now. I know very little about HTML and then it would be simpler to just convert the csv files to txt format and read them instead. – Eivind Jun 09 '15 at 14:41
  • It's for security reasons - the browser isn't going to let you access any files from the disk without the user's consent, so they have to choose a file. And they do that by using a file input element on the page. If that's the task you have to do, it'd be well worth the little amount of time you have to read up on how to do it. It is not difficult. You could look at the source for the demo page on the Papa Parse website for an example. – Matt Jun 09 '15 at 15:02