【问题标题】:how to handles empty lines when reading CSV files in javascript在javascript中读取CSV文件时如何处理空行
【发布时间】:2017-12-11 15:55:00
【问题描述】:

我在我的 MVc 项目中使用 KnockoutJS 来加载 csv 文件并为导入机制提供简单的验证

工作流程如下:

  • 用户选择一个 CSV 文件(提供示例)。
  • 用户点击了一个按钮(哇哦...)。
  • 客户端代码获取 CSV,对其进行解析,然后将其加载到 KnockoutJS 数组中。 一切正常完美我可以上传文件但我的代码加载csv文件中的空行的问题,我不希望用户在导入文件之前删除空行手册

这是 csv 行的示例:

Account_id,External_id,Amount,Transaction_Date,Office_date,Bank,Receipt_nbr,Type,statement,receipt,

0559394,,5,6/20/2017,7/7/2017,Cash,1729002903597,PNL,172900290,3597,

0099952,,19,6/20/2017,7/7/2017,Cash,1729002903653,PNL,172900290,3653,

,,,,,,,,,,

,,,,,,,,,,

,,,,,,,,,,

这是我用来上传文件的代码:

$('#lnkUpload').click(function () {
    var FileToRead = document.getElementById('UserFile');
    if (FileToRead.files.length > 0) {
        var reader = new FileReader();
        // assign function to the OnLoad event of the FileReader  
        // non synchronous event, therefore assign to other method  
        reader.onload = Load_CSVData;
        // call the reader to capture the file  
        reader.readAsText(FileToRead.files.item(0));
    }
    self.userModel.removeAll();

});

 function Load_CSVData(e) {
    self.userModel.removeAll();


    CSVLines = e.target.result.split(/\r\n|\n/);
    $CSVLines = CSVLines.slice(1);
    $.each($CSVLines, function (i, item) {

        var element = item.split(","); // builds an array from comma delimited items  
        var LAccount_id = (element[0] == undefined) ? "" : element[0].trim();
        var LExternal_id = (element[1] == undefined) ? "" : element[1].trim();
        var LAmount = (element[2] == undefined) ? "" : element[2].trim();
        var LTransaction_date = (element[3] == undefined) ? "" : element[3].trim();
        var LOffice_date = (element[4] == undefined) ? "" : element[4].trim();
        var LBank = (element[5] == undefined) ? "" : element[5].trim();
        var LReceipt_nbr = (element[6] == undefined) ? "" : element[6].trim();
        var LType = (element[7] == undefined) ? "" : element[7].trim();
        var Lstatement = (element[8] == undefined) ? "" : element[8].trim();
        var Lreceipt = (element[9] == undefined) ? "" : element[9].trim();






        self.userModel.push(new userModel()
            .Account_id(LAccount_id)
            .External_id(LExternal_id)
            .Amount(LAmount)
            .Transaction_date(LTransaction_date)
            .Office_date(LOffice_date)
            .Bank(LBank)
            .Receipt_nbr(LReceipt_nbr)
            .Type(LType)
            .statement(Lstatement)
            .receipt(Lreceipt))

    });

}

如何将代码更新为忽略并跳过空行或将它们视为输入文件的结尾或任何其他建议

【问题讨论】:

  • if (item.length <= 10) { ... 在循环中是跳过逗号或空行的一种方法。

标签: javascript asp.net-mvc csv knockout.js


【解决方案1】:

您可以检测当前行是否为换行符。

if(item == "\n") { continue; }

编辑:

正如马特指出的那样,如果空格由空格组成,则上述解决方案将不起作用。只需擦除任何空格即可检测该行是否由空格组成。

if(item == "\n" || item.trim().length == 0) { continue; }

【讨论】:

  • 这适用于全空行,但如果一行仅包含空格,则此解决方案将不起作用。
  • if (item == "\n" || item.trim().length == 0)
  • 感谢它的工作 ==> (item == "\n" || item.trim().length
【解决方案2】:

这将检测仅包含逗号和/或空格的行(因此将匹配“\n”、“”、“、、、、”、“、、、、、”等,但不匹配“ ,,,,1,,,,,"

if (item.match(/^[,\s]*$/)) { continue; }

【讨论】:

    猜你喜欢
    • 2011-03-27
    • 2019-12-03
    • 2011-09-27
    • 2020-07-12
    • 1970-01-01
    • 2022-01-21
    • 2019-06-01
    • 2015-10-10
    • 2017-07-09
    相关资源
    最近更新 更多