4

Can someone point to or show me a working example of Papa Parse reading a csv file. When I try to use :

Papa.parse(file, {
    complete: function(results) {
        console.log("Finished:", results.data);
    }
});

the file name is returned in the array instead of the data within. None of the internet examples actually work. The official demo works correctl inspecting its code I cant find it making use of the above strangely.

Sach
  • 75
  • 1
  • 10
  • Give a closer read to the documentation. You don't pass in a file name, you pass in a DOM File object. – Matt Mar 02 '16 at 06:54

2 Answers2

1

As @Matt mentioned in his comment, the trick is not to pass a file name, but a file object. This also was not intuitive to me at first, so here is a quick solution:

var data;

function parse() {
    var file = document.getElementById('myDOMElementId').files[0];

    Papa.parse(file, {
      header: true,
      dynamicTyping: true,
      complete: function(results) {
        console.log("Finished:", results.data);
        data = results.data;
      }
    });
}

Note that you have to call the results in this way when working with a local file. If you want to work with the results elsewhere, assign it to a global variable.

thenaturalist
  • 787
  • 2
  • 8
  • 15
0

I have faced the same problem and it was solved by 2 actions: 1- Adding a callback function 2- connecting to a local oython server/changing browser's security settigns

Check this: https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally

I did not pass an object but a string with the file name/path and it worked for me.