Search code examples
iosreact-nativepapaparse

Papa Parse CSV for React Native


Using PapaParse, I am trying to parse a CSV located locally on an iOS device. The sample code below is pretty straight forward except that it does not take a file path. I do have the path to the local file but I'm not sure how properly insert that in place of fileInput.files[0]. I tried using File() to create a file from the path but could not get anywhere. How can I parse a local csv, in react native using PapaParse?

Papa.parse(fileInput.files[0], {
    complete: function(results) {
        console.log(results);
    }
});

Solution

  • UPDATE: You must first import the file, just as you would a component.

    import myDataset from '../path/to/myDataset.csv';
    

    Then you will use myDataset as your file to download with Papa.parse like so...

    Papa.parse(myDataset, {
        download: true,
        delimiter: '\t'
        complete: function(results) {
            console.log(results);
        }
    });
    

    Specify a config value for download as true. Delimiter should be auto-detected, as per Papa Parse documentation.