Papa Parse reading CSV locally

一笑奈何 提交于 2019-12-23 16:27:58

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/35737645/papa-parse-reading-csv-locally

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!