1

我有一个 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);
}

最好的方法是什么?

4

2 回答 2

4

首先,这在我看来不像 CSV 文件。其次,我会逐行阅读整个文件。当你得到一个像“Processname:;ABC Buying”这样的行时创建一个新对象,它看起来像你对象的第一行。然后为每一行解析它并使用该行上的任何信息修改您的对象。当您到达另一个“Processname:;ABC Buying”时,将您一直在处理的对象保存到您的结果列表中并创建您的新对象。

您的问题没有足够的细节来详细介绍如何解析行等,但以上是我将使用的方法,我怀疑您会变得更好。值得注意的是,这几乎就是您所拥有的,除了将文件拆分为与每个对象相对应的行之外,我会在您进行操作时进行拆分。

于 2013-06-19T16:07:59.537 回答
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,Bob 叔叔可能会提供另一种方法,这更符合您的喜好。

这种方法更喜欢局部变量而不是将变量传入和传出方法。这背后的想法是您很快就会意识到您的班级在内部移动了多少数据。如果您声明了太多变量,则表明发生了太多变量。它也更喜欢较短的方法而不是较长的方法。

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
}

这种方法可能需要一段时间才能习惯,因为您没有传入和传出变量,但它流动得非常好。

于 2013-06-19T16:38:14.263 回答