如何使用 Papa Parse 读取本地文件?我本地有一个文件名为challanges.csv
,但经过多次尝试后我无法用 Papa Parse 解析它。
var data;
Papa.parse('challanges.csv', {
header: true,
dynamicTyping: true,
complete: function(results) {
console.log(results);
data = results.data;
}
});
据我所知,我在将 csv 文件作为 File 打开时遇到问题。我怎样才能用javascript做到这一点?
The File
papaparse 文档建议的 API 适用于所使用的浏览器。假设您在服务器端的节点上运行它,对我有用的是利用可读流 https://nodejs.org/api/fs.html#fs_class_fs_readstream:
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.');
}
});
可能有一个更简单的界面,但到目前为止,它运行得很好,并提供了流处理大文件的选项。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)