读取文本文件并检查列数对于所有行都是相同的

我有一个txt文件,其中包含n个行,每行有n个列,带有一个定界符.

如何逐行读取该txt文件并检查所有行是否具有相同的列.如果任何行有多余的列,则用文本显示行号.

假设我的txt文件中有以下行

147789-00,67,KB08,2007,12,0.000 ,0.000 ,0.000

A22951,67,RN3W,2007,12,0.000 ,0.000 ,0.000

946106-00,67,RN1W,2007,12,0.000 ,0.000 ,0.000,000

A22951,67,RN3W,2007,12,0.000 ,0.000 ,0.000

在第4行中,第3行有额外的列,同样地,可能有一些行可以有额外的列.我想找到的多余的列.或者我可以说如果任何行都有额外的定界符,那么它将显示带有文本的行号.

foreach (string line in File.ReadLines(@"c:\file.txt", Encoding.UTF8))
{
       // how to match the columns    
}

我走的路正确吗?请有人帮助我.

解决方法:

如果您不知道实际的列数,但要确保所有行的未知数都相同:

  char delimiter = ',';
  int columnCount = -1; // or put the number if it's known

  var errors = File
    .ReadLines(@"c:\file.txt", Encoding.UTF8) // UTF-8 is default and can be skipped
    .Select((line, index) => {
      int count = line.Split(delimiter).Length;

      if (columnCount < 0)
        columnCount = count;

      return new {
        line = line,
        count = count,
        index = index
      };
    })
    .Where(chunk => chunk.count != columnCount)
    .Select(chunk => String.Format("Line #{0} \"{1}\" has {2} items when {3} expected",
       chunk.index + 1, chunk.line, chunk.count, columnCount));

 // To check if file has any wrong lines:
 if (errors.Any()) {
   ...
 }

 // To print out a report on wrong lines
 Console.Write(String.Join(Envrironment.NewLine, errors));
上一篇:字符串方法总结


下一篇:php-下载速度比readfile慢很多