How to read data From *.CSV file using javascript?

后端 未结 13 934
不知归路
不知归路 2020-11-22 00:46

My csv data looks like this:

heading1,heading2,heading3,heading4,heading5,value1_1,value2_1,value3_1,value4_1,value5_1,value1_2,value2_2,value3_2,val

13条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 01:07

    Actually you can use a light-weight library called any-text.

    • install dependencies
    npm i -D any-text
    
    • use custom command to read files
    var reader = require('any-text');
     
    reader.getText(`path-to-file`).then(function (data) {
      console.log(data);
    });
    

    or use async-await :

    var reader = require('any-text');
     
    const chai = require('chai');
    const expect = chai.expect;
     
    describe('file reader checks', () => {
      it('check csv file content', async () => {
        expect(
          await reader.getText(`${process.cwd()}/test/files/dummy.csv`)
        ).to.contains('Lorem ipsum');
      });
    });
    

提交回复
热议问题