Papa Parse CSV for Reagent
Using PapaParse , I am trying to parse a CSV installed locally on an iOS device. The example code below is pretty straight forward except that it doesn't take a file path. I have a local file path, but I'm not sure how to properly insert this instead fileInput.files[0]
. I tried using File () to create a file from the path, but couldn't get there. How can I parse local csv, react native with PapaParse?
Papa.parse(fileInput.files[0], {
complete: function(results) {
console.log(results);
}
});
source to share
UPDATE: you must first import the file the same as the component.
import myDataset from '../path/to/myDataset.csv';
Then you will use myDataset as the file to download from Papa.parse like this ...
Papa.parse(myDataset, {
download: true,
delimiter: '\t'
complete: function(results) {
console.log(results);
}
});
Specify the config value for download
as true. The delimiter should be automatically detected according to the Para Parse documentation.
source to share
Just add to @pnizzle's answer above.
For some reason, you might be able to access image files if they are in your React Native project directory, with a relative path from your current js file.
For the CSV file, I had to link them to my iOS project (I grouped the files in the Assets folder), then use RNFS to get the path to my MainBundleDirectory and get the .CSV files from it.
Hope this helps someone else with a similar binding.
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);
}
});
source to share