0

我目前有一个这种格式的分隔文件(3 列选项卡“\t”分隔)和“;” 分隔列中的所有元素)。

    COL1\tCOL2\tCOL3
    abc\t123;1q\tapple\t
    dfg\t234;2w\tapple;apple\t
    hij\t345;3e\tbanana;apple;cherry;\t
    klm\t456;4r\tapple;banana;cherry;banana;cherry;\t
    nop\t567;5t\t;;apple;banana;cherry;banana;;cherry;;\t

我想对优化的宏(理想情况下是 javascript)有任何想法来操作文件以输出:第 3 列现在已排序(也删除了任何额外/不需要的分隔符)并删除了重复项。新的第 4 列是去重元素计数。

    abc\t123;1q\tapple\t1
    dfg\t234;2w\tapple\t1
    hij\t345;3e\tapple;banana;cherry\t3
    klm\t456;4r\tapple;banana;cherry\t3
    nop\t567;5t\tapple;banana;cherry\t3

我一直在尝试类似于下面的方法,但我认为这种方法可能会更快。

    for( iRow = 2; iRow <= totalLines; iRow++ ) { //traverse eash row, start at 2nd row
      str = document.GetCell(iRow, 2, eeCellIncludeQuotes);
      var count = (str.match(/;/g) || []).length;
      var numOfElements = count + 1;
      document.SetCell( iRow, 3, numOfElements, eeAutoQuote );
    }

所以用户应该选择他们想要运行的列(本例中的第 3 列),宏将只在该列上运行,并将计数输出到右侧的新列。

真正的源文件将有数百万行,所以如果这可以通过 EmEditor 以任何方式进行优化,那就太好了。

4

1 回答 1

1

我通过创建一个函数来计算字符串中的分号而不是使用正则表达式(第二版)来优化您的宏,并且还使用GetColumnSetColumn提高速度的方法(第三版)。第三个版本将插入一列而不是覆盖现有的列。

  1. 原始宏(为正确性和时间而修改)

    var start = new Date().getTime();
    
    var totalLines = document.GetLines();
    for( iRow = 2; iRow <= totalLines; iRow++ ) { //traverse eash row, start at 2nd row
        str = document.GetCell(iRow, 3, eeCellIncludeQuotes);
        var count = (str.match(/;/g) || []).length;
        var numOfElements = count + 1;
        document.SetCell( iRow, 4, numOfElements, eeAutoQuote );
    }
    
    var end = new Date().getTime();
    var time = end - start;
    alert( "Execution time: " + time + " ms" );
    
  2. 第二版

    function CountSemiColon( str )
    {
        var count = 0;
        for( var index = -1; ; ) {
            index = str.indexOf( ';', index + 1 );
            if( index == -1 ) {
                break;
            }
            ++count;
        }
        return count;
    }
    
    var start = new Date().getTime();
    
    var totalLines = document.GetLines();
    for( iRow = 2; iRow <= totalLines; iRow++ ) { //traverse eash row, start at 2nd row
        var str = document.GetCell(iRow, 3, eeCellIncludeQuotes);
        document.SetCell( iRow, 4, CountSemiColon( str ) + 1, eeAutoQuote );
    }
    
    var end = new Date().getTime();
    var time = end - start;
    alert( "Execution time: " + time + " ms" );
    
  3. 第三版

    function CountSemiColon( str )
    {
        var count = 0;
        for( var index = -1; ; ) {
            index = str.indexOf( ';', index + 1 );
            if( index == -1 ) {
                break;
            }
            ++count;
        }
        return count;
    }
    
    var start = new Date().getTime();
    
    var totalLines = document.GetLines();
    s1 = document.GetColumn( 3, "\n", eeCellIncludeQuotesAndDelimiter, 2, totalLines - 1 );
    sLines = s1.split( "\n" );
    s2 = "";
    nTotal = sLines.length;
    for( y = 0; y < nTotal; y++ ) {
        s2 += CountSemiColon( sLines[y] ) + 1 + "\n";
    }
    x = s2.length;
    if( x > 0 ) s2 = s2.substr( 0, x - 1 );
    document.InsertColumn( 4, s2, "\n", eeDontQuote, 2 );
    
    var end = new Date().getTime();
    var time = end - start;
    alert( "Execution time: " + time + " ms" );
    
  4. 第四个版本(为空单元格返回 0)

    function CountElements( str )
    {
        if( str.length == 0 || str == '\t' ) {   // if empty string or delimiter only, return 0
            return 0;
        }
        var count = 0;
        for( var index = -1; ; ) {
            index = str.indexOf( ';', index + 1 );
            if( index == -1 ) {
                break;
            }
            ++count;
        }
        return count + 1;   // add 1 to the Count
    }
    
    var start = new Date().getTime();
    
    var totalLines = document.GetLines();
    s1 = document.GetColumn( 3, "\n", eeCellIncludeQuotesAndDelimiter, 2, totalLines - 1 );
    sLines = s1.split( "\n" );
    s2 = "";
    nTotal = sLines.length;
    for( y = 0; y < nTotal; y++ ) {
        s2 += CountElements( sLines[y] ) + "\n";
    }
    x = s2.length;
    if( x > 0 ) s2 = s2.substr( 0, x - 1 );
    document.InsertColumn( 4, s2, "\n", eeDontQuote, 2 );
    
    var end = new Date().getTime();
    var time = end - start;
    alert( "Execution time: " + time + " ms" );
    

试验结果:

  1. 10429 毫秒
  2. 8496 毫秒
  3. 1803 毫秒
  4. 1890 毫秒

100 万行,52 MB CSV 文件。

如果这还不够快,或者出现“Out of Memory”错误,我会考虑其他方法或进一步优化,所以请告诉我。

于 2020-09-15T19:37:47.850 回答