且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从CSV C#中删除空白行

更新时间:2023-12-03 19:56:22

您可以通过用,拆分每行并比较前两列来过滤行.

You could filter the rows by splitting each row with , and comparing first two columns.

var nonEmptyLines = File.ReadAllLines(fileName)
                        .Where(x=> !x.Split(',')
                                     .Take(2)
                                     .Any(cell=> string.IsNullOrWhiteSpace(cell))
                                     // use `All` if you want to ignore only if both columns are empty.  
                         ).ToList();

File.WriteAllLines(fileName, nonEmptyLines);