Papa Parse reading CSV locally

2019-07-18 16:34发布

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.

标签: csv papaparse
2条回答
贼婆χ
2楼-- · 2019-07-18 16:57

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.

查看更多
Anthone
3楼-- · 2019-07-18 17:06

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.

查看更多
登录 后发表回答