爸爸解析本地读取CSV [英] Papa Parse reading CSV locally

查看:87
本文介绍了爸爸解析本地读取CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指向我或向我展示Papa Parse读取csv文件的工作示例. 当我尝试使用时:

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.

推荐答案

正如@Matt在他的评论中提到的,诀窍不是传递文件名,而是传递文件对象.一开始这对我来说也不是很直观,所以这是一个快速的解决方案:

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.

这篇关于爸爸解析本地读取CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆