我有一个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);
}
最好的方法是什么?
答案 0 :(得分:4)
首先,这对我来说看起来不像是一个CSV文件。其次,我只是逐行阅读整个文件。当你得到像“Processname:; ABC Buying”这样的行时创建一个新对象,它看起来像是你对象的第一行。然后为每一行解析它并使用该行上的任何信息修改您的对象。当您到达另一个“Processname:; ABC Buying”时,将您正在处理的对象保存到结果列表中并创建新对象。
你的问题没有足够的细节来详细说明如何解析行等等,但上面是我会使用的方法,我怀疑你会好得多。值得注意的是,除了将文件拆分为与每个对象相对应的行,而不是将文件拆分为相同的符号时,我才会注意到这一点。
答案 1 :(得分:2)
我要做的是拥有一个强类型对象来保存这些数据,以及一个解析器,它接受一个字符串并将其分解为单独的项:
// Has no behaviour - only properties
public class Record
{
public string ID { get;set;}
// Other fields
}
// ------------------
// Only has methods ...
public class RecordParser
{
private string content;
public RecordParser(string content)
{
this.content = content;
}
public IEnumerable<Record> SplitRecords()
{
var list = new List<Record>();
foreach(string section in this.content.Split(/* ... */))
{
var record = CreateRecordFromSection(section);
list.Add(record);
}
return list;
}
private static Record CreateRecordFromSection(string content)
{
StringBuilder currentText = new StringBuilder(content);
var record = new Record()
{
ID = SetId(currentText),
ProcessName = SetProcessName(currentText),
/* Set other properties **/
};
return record;
}
/* Methods for specific behaviour **/
/* Modify the StringBuilder after you have trimmed the content required from it */
private static string SetProcessName(StringBuilder content) { }
private static int SetID(StringBuilder content) { }
/** Others **/
}
从阅读Clean Code开始,鲍勃叔叔可能会提供另一种方法,这更符合您的喜好。
这种方法更倾向于局部变量,而不是将变量传入和传出方法。这背后的想法是你很快意识到你的班级在内部移动数据的程度。如果你声明了太多变量,那就表明发生了太多变化。它也比较长的方法更喜欢更短的方法。
public class RecordParser
{
private List<Record> records;
private Record currentRecord;
private string allContent;
private string currentSection;
public RecordParser(string content)
{
this.allContent = content;
}
public IEnumerable<Record> Split()
{
records = new List<Record>();
foreach(string section in GetSections())
{
this.currentSection = section;
this.currentRecord = new Record();
ParseSection();
records.Add(currentRecord);
}
return records;
}
private IEnumerable<string> GetSections()
{
// Split allContent as needed and return the string sections
}
private void ParseSection()
{
ParseId();
ParseProcessName();
}
private void ParseId()
{
int id = // Get ID from 'currentRecord'
currentRecord.ID = id;
}
private void ParseProcessName()
{
string processName = // Get ProcessNamefrom 'currentRecord'
currentRecord.ProcessName = processName;
}
/** Add methods with no parameters and use local variables
}
这种方法可能需要一段时间才能习惯,因为你没有传入和传出变量,但它流动得非常好。