用于 React Native 的 Papa 解析 CSV [英] Papa Parse CSV for React Native

查看:96
本文介绍了用于 React Native 的 Papa 解析 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PapaParse,我正在尝试解析位于 iOS 设备本地的 CSV.下面的示例代码非常简单,只是它不采用文件路径.我确实有本地文件的路径,但我不确定如何正确插入它来代替 fileInput.files[0].我尝试使用 File() 从路径创建一个文件,但无法到达任何地方.如何使用 PapaParse 解析本地 csv 以响应本机?

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

推荐答案

更新:您必须首先导入文件,就像导入组件一样.

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

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

然后你将使用 myDataset 作为你的文件,像这样用 Papa.parse 下载...

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

download 的配置值指定为 true.根据 Papa Parse 文档,应自动检测分隔符.

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

这篇关于用于 React Native 的 Papa 解析 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆