0

我有一个csv文件,其中一行有时有一列有两个值。目前我正在尝试使用拆分,,但没有按预期工作。有人可以阐明如何实现预期的输出。我不想使用任何npm库。

//users.csv
name,age,hobbies
james,20,"reading,playing"
marie,18,singing
peter,19,watching movies

//index.js
const fs = require('fs');
const {promisify} = require("util");
const readFile = promisify(fs.readFile)
const readSheet = async () =>{
    const result = await readFile("./users.csv", "utf-8");
    const csv = result.split("\n").map(ele=>ele.split(","))
    let keys = csv.slice(0,1)[0];
    const values  = csv.slice(1)
    keys.map(keys=>keys.split(''))
    let usersArr = [];
    for(let i = 0;i<values.length;i++){
        let usersObj = {};
        keys.forEach((key,j)=>usersObj[key] = values[i][j])
        usersArr.push(usersObj)
    }
    return usersArr


}
//current output
[ { name: 'james', age: '20', hobbies: '"reading' },
  { name: 'marie', age: '18', hobbies: 'singing' },
  { name: 'peter', age: '19', hobbies: 'watching movies' } ]
  //expected output object
[
 {
   "name": "james",
   "age": 20,
   "hobbies": ["reading","playing"]
 },
 {
   "name": "marie",
   "age": 18,
   "hobbies": ["singing"]
 },
 {
   "name": "peter",
   "age": 19,
   "hobbies": ["watching movies"]
 }
]


4

2 回答 2

-1

用逗号分隔每一行是行不通的,因为引号中有逗号。您最好的选择是使用来自npm诸如fast-csv. 由于您不想使用库,因此可以使用正则表达式来拆分行,如本问题所述:

这是一个示例工作代码(请注意限制):

const fs = require('fs');
const {promisify} = require("util");
const readFile = promisify(fs.readFile)

const CSV_CONTENT = "name,age,hobbies\njames,20,\"reading,playing\"\nmarie,18,singing\npeter,19,watching movies\n";

const readSheet = async () => {
    //const lines = await readFile("./users.csv", "utf-8");

    const lines = CSV_CONTENT.split("\n")
                              .filter(l => !!l) // filter empty lines

    let keys = lines.slice(0,1)[0].split(',') // assuming there's no quotes in keys
    const valueLines  = lines.slice(1)

    let usersArr = [];
    for(let i=0; i<valueLines.length; i++) {
        let usersObj = {};
        let values = splitLine(valueLines[i]); // splitLine handles quotes
        keys.forEach((key,j) => usersObj[key] = trimQuotes(values[j]))
        usersArr.push(usersObj)
    }
    return usersArr

}

function splitLine(line) {
        var matches = line.match(/(\s*"[^"]+"\s*|\s*[^,]+|,)(?=,|$)/g);
        for (var n = 0; n < matches.length; ++n) {
            matches[n] = matches[n].trim();
            if (matches[n] == ',') matches[n] = '';
        }
        if (line[0] == ',') matches.unshift("");
        return matches;
}

function trimQuotes(input) {
  let expr = /^(")?(.*?)(")?$/g
  let groups = expr.exec(input)
  return groups[2]
}

(async () => {
  let result = await readSheet();
  console.log(result);
})();
于 2019-11-18T03:06:31.350 回答
-1

您可以使用这篇博客文章中描述的 csv 函数:https ://www.bennadel.com/blog/1504-ask-ben-parsing-csv-strings-with-javascript-exec-regular-expression-command.htm

之后将数组转换为对象:

let csv = ... // csv data
for(let i=1;i<csv.length;i++){
    let obj = {};
    for(let b in csv[i]){
        obj[csv[0][b]] = csv[i][b];
    }
    csv[i] = obj;
}
csv.shift(); // remove the first row (headers)

之后只需用逗号分割数组中的爱好列:

for(let entry of csv){
    csv.hobbies = csv.hobbies.split(','); 
}
于 2019-11-18T02:55:38.557 回答