如何修复CSV到JSON转换器模块?

时间:2019-03-28 16:39:51

标签: javascript node.js

我无法根据模块中的内容正确匹配标题和体裁。

csv_json模块有一个例外,即它与相应的每个属性都不匹配,也就是说,标题中包含“ The”。

//csv file

movieId,标题,类型

1,“美国总统,(1995年)”,喜剧|戏剧|浪漫 2,“创造,创造者(xxxx)”,喜剧|戏剧|浪漫 3,“破坏,驱逐舰(xxxxx)”,喜剧|戏剧|浪漫


//csv_json module 
const readline = require('readline');
const fs = require('fs');

function readCsv(pathToFile) {
    return new Promise((resolve, reject) => {
        const csvReader = readline.createInterface({
            input: fs.createReadStream(pathToFile)
        });

        let headers;
        const rows = [];
        let tempRows = [];
        csvReader
            .on('line', row => {
                if (!headers) {
                    headers = row.split(','); // header name breed age
                } else {
                    rows.push(row.split(','));
                }
            })
            .on('close', () => {
                // then iterate through all of the "rows", matching them to the "headers"
                for (var i = 0; i < rows.length; i++) {
                    var obj = {};
                    var currentline = rows[i];

                    for (var j = 0; j < headers.length; j++) {
                        obj[headers[j]] = currentline[j]; //Kitty Siamese 14
                    }

                    tempRows.push(obj);
                }

                resolve(JSON.stringify(tempRows));
            });

        // This would be in place of the "return" statement you had before
    });
}

module.exports = readCsv;
//js file
const readCsv = require('./csvjson.js');

readCsv('movieTest.csv').then((data) => {
    console.log(data)
    let movieJson = JSON.parse(data);
    console.log(movieJson)


/*data output:
[{"movieId":"1","title":"\"American President","genre":" The (1995)\""},{"movieId":"2","title":"\"Creation","genre":" The creator(xxxx)\""},{"movieId":"3","title":"\"Destruction","genre":" The destroyer(xxxxx)\""}]

*/
/*movieJson output:
[ { movieId: '1',
    title: '"American President',
    genre: ' The (1995)"' },
  { movieId: '2',
    title: '"Creation',
    genre: ' The creator(xxxx)"' },
  { movieId: '3',
    title: '"Destruction',
    genre: ' The destroyer(xxxxx)"' } ]
*/
});

我希望输出匹配:

[ { movieId: '1',
    title: "American President, The (1995)",
    genre:'Comedy|Drama|Romance' },
  { movieId: '2',
    title: "The creator(xxxx) Creation",
    genre: ' Comedy|Drama|Romance' },
  { movieId: '3',
    title: "Destruction The destroyer(xxx)",
    genre: ' Comedy|Drama|Romance' } ]

1 个答案:

答案 0 :(得分:1)

这可能是因为您要在每次出现逗号时将每一行拆分。

const row = '1,"American President, The (1995)",Comedy|Drama|Romance'
row.split(',')
// returns ["1", ""American President", " The (1995)"", "Comedy|Drama|Romance"]

尝试用CSV文件中其他任何地方都不会出现的唯一字符串替换每个逗号后没有空格的逗号,然后在该字符串上拆分:

row.replace(/\,(\S)/g, '&unique;$1').split('&unique;')
// returns ["1", ""American President, The (1995)"", "Comedy|Drama|Romance"]

希望这会有所帮助! :)