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-parser
或node-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对象数组。
node.js 读取文件名