1 回答
TA贡献1828条经验 获得超4个赞
此代码应以您提到的格式解析 CSV 文件,例如
测试.csv
name,emails
name1,email1
name2,email2
name3,email3
name4,email4
name5,email5
应用程序.js
const Papa = require('papaparse');
const fs = require("fs");
let csvData = fs.readFileSync("test.csv", "utf-8");
csvData = csvData.trim().replace(/([^,]+),\s*([^,]+),\s*/gi, '$1,$2\n');
console.log("Adjusted csv:", csvData);
let { meta, data } = Papa.parse(csvData, { header: true });
console.log("Parsed data:", data);
console.log("Metadata:", meta);
解析后的数据如下所示:
[
{ name: 'name1', emails: 'email1' },
{ name: 'name2', emails: 'email2' },
{ name: 'name3', emails: 'email3' },
{ name: 'name4', emails: 'email4' },
{ name: 'name5', emails: 'email5' }
]
添加回答
举报