React:导入CSV文件并解析 [英] React: import csv file and parse

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

问题描述

我有以下名为data.csv的csv文件,它与我的js控制器位于同一文件夹中:

I have the following csv file that is named data.csv and located in the same folder as my js controller:

namn,vecka,måndag,tisdag,onsdag,torsdag,fredag,lördag,söndag
Row01,a,a1,a2,a3,a4,a5,a6,a7
Row02,b,b1,b2,b3,b4,b5,b6,b7
Row03,c,c1,c2,c3,c4,c5,c6,c7
Row04,d,d1,d2,d3,d4,d5,d6,d7
Row05,e,e1,e2,e3,e4,e5,e6,e7
Row06,f,f1,f2,f3,f4,f5,f6,f7
Row07,g,g1,g2,g3,g4,g5,g6,g7
Row08,h,h1,h2,h3,h4,h5,h6,h7
Row09,i,i1,i2,i3,i4,i5,i6,i7
Row10,j,j1,j2,j3,j4,j5,j6,j7
Row11,k,k1,k2,k3,k4,k5,k6,k7
Row12,l,l1,l2,l3,l4,l5,l6,l7

在反应中,我需要导入一个csv文件并将其解析为JSON.我尝试了以下方法:

In react, I need to import a csv file and parse it to JSON. I tried the following:

componentWillMount() {
    this.getCsvData();
}

getData(result) {
    console.log(result);
}

getCsvData() {
    let csvData = require('./data.csv');

    Papa.parse(csvData, {
        complete: this.getData
    });
}

由于某些原因,这不起作用.第一个console.log显示/static/media/data.0232d748.csv,第二个显示以下内容:

This doesnt work for some reason. The first console.log displays /static/media/data.0232d748.csv and the second displays the following:

{
  "data": [
    [
      "/static/media/horoscope-data.0232d748.csv"
    ]
  ],
  "errors": [
    {
      "type": "Delimiter",
      "code": "UndetectableDelimiter",
      "message": "Unable to auto-detect delimiting character; defaulted to ','"
    }
  ],
  "meta": {
    "delimiter": ",",
    "linebreak": "\n",
    "aborted": false,
    "truncated": false,
    "cursor": 41
  }
}

我不明白我在做什么错.有人可以帮我吗?

I don't understand what I am doing wrong. Can someone help me?

推荐答案

要回答我自己的问题,我可以像这样重写它(/src/controllers/data-controller/data-controller.js,添加了完整的代码以提高清晰度):

To answer my own question, I was able to rewrite it like this (/src/controllers/data-controller/data-controller.js, added the full code for better clarity):

import React from 'react';
import Papa from 'papaparse';
import {withRouter} from 'react-router-dom';

class DataController extends React.Component {

    constructor(props) {
        super(props);

        this.state = {
            data: []
        };

        this.getData = this.getData.bind(this);
    }

    componentWillMount() {
        this.getCsvData();
    }

    fetchCsv() {
        return fetch('/data/data.csv').then(function (response) {
            let reader = response.body.getReader();
            let decoder = new TextDecoder('utf-8');

            return reader.read().then(function (result) {
                return decoder.decode(result.value);
            });
        });
    }

    getData(result) {
        this.setState({data: result.data});
    }

    async getCsvData() {
        let csvData = await this.fetchCsv();

        Papa.parse(csvData, {
            complete: this.getData
        });
    }

    render() {
        return (
            <section className="data-controller">
                ...
            </section>
        );
    }
}

export default withRouter(DataController);

在这里,我使用内置的获取功能像流一样获取它,然后使用内置的流读取器读取流,并使用TextDecoder解码数据.我也不得不移动文件.在将其放置在中之前,但我不得不将其移至/public/data.

Here I use the built in fetch to get it like a stream and then read the stream using the build in stream reader, and decode the data using TextDecoder. I had to move the file also. Before it was located in /src/controllers/data-controller but I had to move it to /public/data.

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

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