简体   繁体   English

如何使用 Papa Parse 读取本地文件?

[英]How can I read a local file with Papa Parse?

How can I read a local file with Papa Parse?如何使用 Papa Parse 读取本地文件? I have a file locally called challanges.csv , but after many tried I can't parse it with Papa Parse.我在本地有一个名为challanges.csv的文件,但经过多次尝试后,我无法使用 Papa Parse 解析它。

var data;

Papa.parse('challanges.csv', {
  header: true,
  dynamicTyping: true,
  complete: function(results) {
    console.log(results);
    data = results.data;
  }
});

As far as I know, I'm having problems with opening the csv file as File.据我所知,我在将 csv 文件作为 File.open 打开时遇到了问题。 How can I do it with javascript?我怎样才能用 javascript 做到这一点?

The File API suggested by papaparse's docs is meant for browser used. papaparse's docs 建议的File API 适用于使用的浏览器。 Assuming that you are running this on node at server side, what works for me is leveraging the readable stream :假设您在服务器端的节点上运行它,对我有用的是利用可读流

const fs = require('fs');
const papa = require('papaparse');
const file = fs.createReadStream('challenge.csv');
var count = 0; // cache the running count
papa.parse(file, {
    worker: true, // Don't bog down the main thread if its a big file
    step: function(result) {
        // do stuff with result
    },
    complete: function(results, file) {
        console.log('parsing complete read', count, 'records.'); 
    }
});

There may be an easier interface, but so far this works quite well and offer the option of streaming for processing large files.可能有一个更简单的界面,但到目前为止,它工作得很好,并提供了处理大文件的流选项。

None of these worked for me, I specifically wanted to read in a csv server side , and not client side (in the borwser).这些都不适合我,我特别想在 csv服务器端阅读,而不是客户端(在浏览器中)。 This worked for me.这对我有用。

async / await异步/等待

const fs = require('fs');
const Papa = require('papaparse');

const csvFilePath = 'data/test.csv'

// Function to read csv which returns a promise so you can do async / await.

const readCSV = async (filePath) => {
  const csvFile = fs.readFileSync(filePath)
  const csvData = csvFile.toString()  
  return new Promise(resolve => {
    Papa.parse(csvData, {
      header: true,
      complete: results => {
        console.log('Complete', results.data.length, 'records.'); 
        resolve(results.data);
      }
    });
  });
};

const test = async () => {
  let parsedData = await readCSV(csvFilePath); 
}

test()

If you don't want promise / async, await then you can use this.如果你不想要承诺/异步,那么你可以使用它。

callback回调

const fs = require('fs');
const Papa = require('papaparse');

const csvFilePath = 'data/test.csv'

const file = fs.createReadStream(csvFilePath);

var csvData=[];
Papa.parse(file, {
  header: true,
  step: function(result) {
    csvData.push(result.data)
  },
  complete: function(results, file) {
    console.log('Complete', csvData.length, 'records.'); 
  }
});

Note header: true is an option on the config, see docs for other options注意header: true是配置中的一个选项,其他选项请参见文档

You need to add one more line in your config: download: true, .您需要在配置中再添加一行: download: true, .

var data;

Papa.parse('../challanges.csv', {
  header: true,
  download: true,
  dynamicTyping: true,
  complete: function(results) {
    console.log(results);
    data = results.data;
  }
});

Update: with this answer you dont need a FILE OBject.更新:有了这个答案,您就不需要 FILE 对象。 You can pass the filename and papa parse will "download" it.您可以传递文件名,papa parse 将“下载”它。

This is to reiterate that the best answer is Murat Seker's.这是为了重申最好的答案是 Murat Seker 的。

All that is necessary is to add the to the config download: true and the local path will be downloaded by Papa Parse.所需要做的就是在配置download: true添加download: true并且本地路径将由 Papa Parse 下载。 The streaming answer by Philip M. is not the best answer. Philip M. 的流媒体回答并不是最好的回答。

var data;

Papa.parse('challanges.csv', {
  header: true,
  download: true,
  dynamicTyping: true,
  complete: function(results) {
    console.log(results);
    data = results.data;
  }
});

PS I do not have enough reputation to comment on Murat Seker's answer. PS 我没有足够的声誉来评论 Murat Seker 的回答。 So, I reposted an answer.所以,我转发了一个答案。 Any love towards reputation will be appreciated.任何对声誉的热爱将不胜感激。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM