在Meteor.js中导入/读取CSV

Import/Read CSV in Meteor.js

本文关键字:读取 CSV 导入 Meteor js      更新时间:2023-09-26

如何从/private目录下的文件系统路径读取CSV文件到Meteor应用程序中?

发现fast-csv是为Meteor打包的,但如何创建createReadStream以供fast-csv包使用

使用Oskar的papa-parse建议,您可以这样做:

$ meteor add harrison:papa-parse

然后在您的服务器上:

// read your file as a csv string (assuming it's in the private dir)
var csv = Assets.getText('path/to/your.csv');
// convert the csv to an array of arrays
var rows = Papa.parse(csv).data;
// show the first row
console.log(rows[0]);

推荐阅读:

  • papa解析文档。
  • 这篇关于使用Assets API的文章。

此外,如果您提前将数据存储在private目录中,我建议将其转换为无需解析即可读取的格式(即使用JSON而不是CSV)。

我将使用PapaParse读取CSV (meteor add harrison:papa-parse),可在这里获得。这是超级容易使用。

你只需要使用

Papa.parse("http://example.com/file.csv", {
download: true,
complete: function(results) {
    console.log(results);
}
});

或者,如果您喜欢循序渐进的方法,您可以这样使用:

Papa.parse("http://example.com/big.csv", {
download: true,
step: function(row) {
    console.log("Row:", row.data);
},
complete: function() {
    console.log("All done!");
}

});

IMHO这是一个非常好的软件包,我已经广泛使用它。更多医生[在这里]。享受吧!2