5

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);
    }
});
pnizzle
  • 6,243
  • 4
  • 52
  • 81

2 Answers2

0

Just to add to Answer above by @pnizzle.

For some reason you can access Image files as long as they are in your React Native project's directory, with a relative path from your current js file.

For CSV file I had to bundle them with my iOS project (I grouped the files in a Assets folder) then use RNFS to get the path for my MainBundleDirectory and access my .CSV files from there.

Hope this helps anyone else in a similar bind.

var mainBundlePath = RNFS.MainBundlePath;
var path = '/Assets/CSVData.csv';
Papa.parse(mainBundlePath+path, {
download: true,
delimiter: '\t',
complete: function(results) {
    console.log('This is ME..');
    console.log(results);
    }
});
InherentlyNuts
  • 199
  • 2
  • 8
-1

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.

valem
  • 1,690
  • 2
  • 9
  • 11
pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • 1
    Is there anything particular you need to do for the import to work? It seems like it doesn't work on my version (0.50.3) even though I can import images (`.png`) this way. – cglacet Jan 18 '18 at 15:27
  • I get an error on trying to import - Unable to resolve module. Do i have to register the CSVs somewhere? – InherentlyNuts Mar 10 '18 at 19:25
  • @InherentlyNuts the CSV has to be either in your iOS / android file system eg the iOS documents directory, or it can be a bundled file, in other words added to your iOS or Android project. You can not use your computers file system path, from my understanding – pnizzle Mar 11 '18 at 23:08
  • 1
    @ChristianG. please see my above comment, maybe that can help. – pnizzle Mar 11 '18 at 23:09
  • strangely I could access all image files saved at the same location, just not csv. This is very strange. – InherentlyNuts Mar 14 '18 at 10:12