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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49752889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:45:01  来源:igfitidea点击:

How can I read a local file with Papa Parse?

javascriptnode.jscsvpapaparse

提问by adamb

How can I read a local file with Papa Parse? I have a file locally called challanges.csv, but after many tried I can't parse it with Papa Parse.

如何使用 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. How can I do it with javascript?

据我所知,我在将 csv 文件作为 File.open 打开时遇到了问题。我怎样才能用 javascript 做到这一点?

回答by Philip M.

The FileAPI suggested by papaparse's docs is meant for browser used. Assuming that you are running this on node at server side, what works for me is leveraging the readable stream:

Filepapaparse's docs 建议的API 适用于使用的浏览器。假设您在服务器端的节点上运行它,对我有用的是利用可读流

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.

可能有一个更简单的界面,但到目前为止,它工作得很好,并提供了处理大文件的流选项。

回答by Glen Thompson

None of these worked for me, I specifically wanted to read in a csv server side, and not client side(in the borwser). This worked for me.

这些都不适合我,我特别想在 csv服务器端阅读,而不是客户端(在浏览器中)。这对我有用。

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: trueis an option on the config, see docs for other options

注意header: true是配置上的一个选项,其他选项请参见文档

回答by Murat Seker

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. You can pass the filename and papa parse will "download" it.

更新:有了这个答案,您就不需要 FILE 对象。您可以传递文件名,papa parse 将“下载”它。

回答by David Bernat

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: trueand the local path will be downloaded by Papa Parse. The streaming answer by Philip M. is not the best answer.

所需download: true要做的就是将 加到配置中,Papa Parse 将下载本地路径。Philip M. 的流媒体回答并不是最好的回答。

var data;

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

P.S. I do not have enough reputation to comment on Murat Seker's answer. So, I reposted an answer. Any love towards reputation will be appreciated. :-)

PS 我没有足够的声誉来评论 Murat Seker 的回答。所以,我转发了一个答案。任何对声誉的热爱将不胜感激。:-)