Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  160] [ 3]  / answers: 1 / hits: 57769  / 6 Years ago, tue, april 10, 2018, 12:00:00

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;
}
});


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


More From » node.js

 Answers
22

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.


[#54703] Saturday, April 7, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lizet

Total Points: 479
Total Questions: 96
Total Answers: 113

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
;