node.js 读取文件名_如何使用Node.js读取CSV文件

node.js 读取文件名

Many different npm modules let you read from a CSV file.

通过许多不同的npm模块,您可以读取CSV文件。

Most of them are based on streams, like csv-parser or node-csv.

它们大多数基于 ,例如csv-parsernode-csv

Those are great to deal with CSV in a production system.

这些非常适合在生产系统中处理CSV。

I like to keep things simple when I don’t have performance in mind. For example, for a one-time parsing of CSV that I had to do to consolidate my backend systems.

当我不考虑性能时,我喜欢保持简单。 例如,对于一次CSV解析,我不得不整合后端系统。

To do so, I used neat-csv, a package that exposes the csv-parser functionality to a simple async/await interface.

为此,我使用了neat-csv ,这是一个将csv-parser功能公开给简单的async / await接口的软件包。

Install it using npm install neat-csv and require it in your app:

使用npm install neat-csv安装它,并在您的应用程序中要求它:

const neatCsv = require('neat-csv');

then load the CSV from the filesystem and invoke neatCsv passing the content of the file:

然后从文件系统加载CSV并调用neatCsv传递文件内容:

const fs = require('fs')

fs.readFile('./file.csv', async (err, data) => {
  if (err) {
    console.error(err)
    return
  }
  console.log(await neatCsv(data))
})

Now you can start doing whatever you need to do with the data, which is formatted as a JavaScript array of objects.

现在,您可以开始处理数据,该数据被格式化为JavaScript对象数组。

翻译自: https://flaviocopes.com/node-read-csv/

node.js 读取文件名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值