检查CSV标头是否匹配,如果匹配则继续分析,否则停止

Check if CSV headers match, if they match continue parsing otherwise stop

本文关键字:继续 CSV 检查 如果 是否      更新时间:2023-09-26

在继续使用PapaParse进行解析之前,我目前正在尝试添加一些关于特定.CSV格式的描述的验证。

所以我的想法是先检查标题,如果它们等同于以下内容:

Extension, company, name

然后继续解析,否则我会返回一条错误消息,说明格式错误。

所有的解析都是使用PapaParse完成的。

Ave对此并不满意,但以下是当前代码:

var result = [];
$("#CSV-Upload").click(function () {
        $("input[type=file]").parse({
            config : {
                header : true,
                skipEmptyLines : true,
                complete : function (results, file) {
                    console.log("This file done:", file, results);
                    var string = JSON.stringify(results['data']);
                    result.push(string);
                    console.log("CSV Array: " + string);
                }
            },
            complete : function () {
                console.log("All files done!");
            }
        });
        $("#csv-file").val('');
    });

如果我理解正确,您需要检查标头中是否存在特定键。要使用papa-parse来完成此操作,我建议使用streaming。在papa-parse中,streaming的概念是在解析器读取数据时处理数据。

基本上,您将在step函数中返回的row对象中检查某个键。查看以下代码:

var allKeyPresent = false; // Flag
Papa.parse(file, {
    header : true,
    skipEmptyLines : true,
    step: function(row, parser){
        if (!allKeyPresent) { //Only chek if flag is not set, i.e, for the first time
            parser.pause(); // pause the parser
            var first_row_data = row.data[0];
            // Now check object keys, if it match
            if (('Extension' in first_row_data) && ('name' in first_row_data) && ('email' in first_row_data)) {
                //every required key is present
                allKeyPresent = true;
                // Do your data processing here
                parser.resume();
            } else{
                //some key is missing, abort parsing
                parser.abort();
            }
        } else{ // we already match the header, all required key is present
            // Do the Data processing here
        }
    }
});

要了解更多关于papa解析流媒体的信息,请查看此。另外,请参阅文档的配置说明部分中有关step函数的更多信息。

我希望这能有所帮助。如果您有任何其他相关疑问,请告诉我。

async importData(path:string){
        const planilha:any[] = [] 
        const unparsed = fs.readFileSync(path, 'utf-8')             
        Papa.parse(unparsed,{   
            header:true,                
            skipEmptyLines:true,
            step:function(row,parser){
            if(row.meta.fields?.length!==4){
                    parser.abort()
                    fs.unlinkSync(path)
                    throw new BadRequestException('a planilha de importação deve ter 4 campos: name, phone,email e cpf nesta ordem e nestes formatos')
                }
                if(row.meta.fields[0]!=='name'){
                    parser.abort()
                    fs.unlinkSync(path)
                    throw new BadRequestException('o primeiro campo da planilha deve ser "name" ')
                }
                if(row.meta.fields[1]!=='phone'){
                    parser.abort()
                    fs.unlinkSync(path)
                    throw new BadRequestException('o segundo campo da planilha deve ser "phone" ')
                }
                if(row.meta.fields[2]!=='email'){
                    fs.unlinkSync(path)
                    throw new BadRequestException('o terceiro campo da planilha deve ser "email" ')
                }
                if(row.meta.fields[3]!=='cpf'){
                    parser.abort()
                    fs.unlinkSync(path)
                    throw new BadRequestException('o quarto campo da planilha deve ser "cpf" ')
                } 
                  
                    planilha.push(row.data)
                    parser.resume()             
                   
            }                               
        })
    fs.unlinkSync(path)

}