我有一个 csv 文件,如下所示,
Processname:;ABC Buying
ID:;31
Message Date:;08-02-2012
Receiver (code):;12345
Object code:
Location (code):;12345
Date;time
2012.02.08;00:00;0;0,00
2012.02.08;00:15;0;0,00
2012.02.08;00:30;0;0,00
2012.02.08;00:45;0;0,00
2012.02.08;01:00;0;0,00
2012.02.08;01:15;0;0,00
它可以有 1 次或多次出现上述消息,所以假设它有 2 次出现,那么 csv 文件看起来像......
Processname:;ABC Buying
ID:;31
Message Date:;08-02-2012
Receiver (code):;12345
Object code:
Location (code):;12345
Date;time
2012.02.08;00:00;0;0,00
2012.02.08;00:15;0;0,00
2012.02.08;00:30;0;0,00
2012.02.08;00:45;0;0,00
2012.02.08;01:00;0;0,00
2012.02.08;01:15;0;0,00
Processname:;ABC Buying
ID:;41
Message Date:;08-02-2012
Receiver (code):;12345
Object code:
Location (code):;12345
Date;time
2012.02.08;00:00;0;17,00
2012.02.08;00:15;0;1,00
2012.02.08;00:30;0;15,00
2012.02.08;00:45;0;0,00
2012.02.08;01:00;0;0,00
2012.02.08;01:15;0;9,00
解析此 csv 文件的最佳方法是什么?
我的方法的伪代码...
// Read the complete file
var lines = File.ReadAllLines(filePath);
// Split the lines at the occurrence of "Processname:;ABC Buying"
var blocks = lines.SplitAtTheOccuranceOf("Processname:;ABC Buying");
// The results will go to
var results = new List<Result>();
// Loop through the collection
foreach(var b in blocks)
{
var result = new Result();
foreach(var l in b.lines)
{
// read the first line and check it contains "Processname" if so, assign the value to result.ProcessName =
// read the 2nd line and check it contains "ID" if so, assign the value to result.ID
// read the 3rd line and check it contains "Object Code" if so, assign the value to result.ObjectCode
// Ignore string.empty
// check for location (code), if so assign the value to result.LocationCode
// Parse all the other rows by spliting with ';' the first part is date, 2nd part is time, 3rd part is value
}
results.Add(result);
}
最好的方法是什么?