如果您将文件作为 CSV 数据读取并手动解析,您可以执行以下操作来查找开头和结尾的第一个非零条目:
// prepare sample data (as CSV)
var csv =
@"id, value1,value2,value3,value4,value5
a, 10,4,2,0,0
b, 23,0,0,2,0
c, 0,2,4,0,0
d, 0,0,0,1,0";
// get column data
var lines = csv.Split('\n');
var rows = lines.Skip(1).Select (
line => line.Split(',').Skip(1).Select (l => Convert.ToInt32(l)));
var result = new List<int>();
foreach (var row in rows)
{
// for each row, remove 0's at beginning and end
var data = row.SkipWhile (r => r == 0).Reverse()
.SkipWhile (r => r == 0).Reverse();
// and retrieve the count
result.Add(data.Count());
}
// result = [3, 4, 2, 1]