如何使用Papa Parse读取本地文件? [英] How can I read a local file with Papa Parse?

查看:674
本文介绍了如何使用Papa Parse读取本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Papa Parse读取本地文件?我在本地有一个名为challanges.csv的文件,但经过多次尝试,我无法使用Papa Parse对其进行解析.

How can I read a local file with Papa Parse? I have a file locally called challanges.csv, but after many tried I can't parse it with Papa Parse.

var data;

Papa.parse('challanges.csv', {
  header: true,
  dynamicTyping: true,
  complete: function(results) {
    console.log(results);
    data = results.data;
  }
});

据我所知,我在打开csv文件作为File时遇到问题.如何使用javascript?

As far as I know, I'm having problems with opening the csv file as File. How can I do it with javascript?

推荐答案

papaparse的文档建议的File API供浏览器使用.假设您正在服务器端的节点上运行此代码,那么对我有用的就是利用可读流:

The File API suggested by papaparse's docs is meant for browser used. Assuming that you are running this on node at server side, what works for me is leveraging the readable stream:

const fs = require('fs');
const papa = require('papaparse');
const file = fs.createReadStream('challenge.csv');
var count = 0; // cache the running count
papa.parse(file, {
    worker: true, // Don't bog down the main thread if its a big file
    step: function(result) {
        // do stuff with result
    },
    complete: function(results, file) {
        console.log('parsing complete read', count, 'records.'); 
    }
});

可能会有一个更简单的界面,但到目前为止,它的运行效果很好,并提供了用于处理大文件的流传输选项.

There may be an easier interface, but so far this works quite well and offer the option of streaming for processing large files.

这篇关于如何使用Papa Parse读取本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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