Skip to content Skip to sidebar Skip to footer

How Can I Read A Local File With 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.c

Solution 1:

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 filestep: 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.

Solution 2:

None of these worked for me, I specifically wanted to read in a csv server side, and not client side (in the borwser). This worked for me.

async / await

const fs = require('fs');
constPapa = require('papaparse');

const csvFilePath = 'data/test.csv'// Function to read csv which returns a promise so you can do async / await.const readCSV = async (filePath) => {
  const csvFile = fs.readFileSync(filePath)
  const csvData = csvFile.toString()  
  returnnewPromise(resolve => {
    Papa.parse(csvData, {
      header: true,
      complete: results => {
        console.log('Complete', results.data.length, 'records.'); 
        resolve(results.data);
      }
    });
  });
};

const test = async () => {
  let parsedData = awaitreadCSV(csvFilePath); 
}

test()

If you don't want promise / async, await then you can use this.

callback

const fs = require('fs');
constPapa = require('papaparse');

const csvFilePath = 'data/test.csv'const file = fs.createReadStream(csvFilePath);

var csvData=[];
Papa.parse(file, {
  header: true,
  step: function(result) {
    csvData.push(result.data)
  },
  complete: function(results, file) {
    console.log('Complete', csvData.length, 'records.'); 
  }
});

Note header: true is an option on the config, see docs for other options

Solution 3:

You need to add one more line in your config: download: true,.

var data;

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

Update: with this answer you dont need a FILE OBject. You can pass the filename and papa parse will "download" it.

Solution 4:

Here is my solution: 1: FE: Angular 10, NPM install ngx-papaparse Then import in the component

import { Papa } from"ngx-papaparse";

2: one input with type file only accept csv file on FE

 <input type="file"id="csv-file" accept=".csv" (change)="onFileSelected($event)">

3: one button to read CSV file

4: we get the file data from the onFileSelected function

const files = event.target.files;
    if (files.length === 0)
      return;
    this.selectedFile = files[0] as File;

5: call read CSV file function

this.papa.parse(this.selectedFile, {
        delimiter: ",",
        newline: "",
        header: true,
        dynamicTyping: true,
        complete: (result) => {
          this.fileData= result.data;
        }
      });

6: you should get the object fileData Tips: check the papaparse documentation to see what configuration you need. https://www.papaparse.com/ eg: header: true --> means you will get the headers in the result object

Solution 5:

This is to reiterate that the best answer is Murat Seker's.

All that is necessary is to add the to the config download: true and the local path will be downloaded by Papa Parse. The streaming answer by Philip M. is not the best answer.

var data;

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

P.S. I do not have enough reputation to comment on Murat Seker's answer. So, I reposted an answer. Any love towards reputation will be appreciated. :-)

Post a Comment for "How Can I Read A Local File With Papa Parse?"