如何通过考虑C#中的选定数据来隔离.csv文件

时间:2017-06-30 07:28:07

标签: c# csv

我有.csv个文件,我希望将所选数据列分开并将这些选定数据写入新的.csv文件。

充电和放电存储在具有固定格式的.csv文件中。每个文件包含2个子项详细信息。此按钮事件处理程序应根据条件将main.csv文件隔离到单独的.csv文件中:

如果仅通过考虑以下条件生成新的.csv文件。

CutMark(1,2)= TRUE,用于特定“Pass”的整个时段

例如:

•如果在“FALSE-TRUE-FALSE-TRUE”(“放电 - 充电 - 放电 - 充电”)的“通过”遍历的整个期间全部处于活动状态(CutMark1和CutMark2都为TRUE),则以下文件应该由这个工具创建[共8个文件]:

Discharge1_CH1.csv(列标题为Date,Pass,CutMarks1,Marks1)

Discharge1_CH2.csv(列标题为Date,Pass,CutMarks2,Marks2)

Charge1_CH1.csv(列标题为Date,Pass,CutMarks1,Marks1)
 Charge1_CH2.csv(列标题为Date,Pass,CutMarks2,Marks2)

Discharge2_CH1.csv(列标题为Date,Pass,CutMarks1,Marks1)Discharge2_CH2.csv(列标题为Date,Pass,CutMarks2,Marks2)

Charge2_CH1.csv(列标题为Date,Pass,CutMarks1,Marks1)

Charge2_CH2.csv(列标题为Date,Pass,CutMarks2,Marks2)

就像我想要生成新的.csv文件一样。 这是我的样本.csv文件:

Date,Pass,CutMark1,Marks1,CutMark2,Marks2,
22/06/2017 13:04:18,FALSE,TRUE,40,FALSE,35,
22/06/2017 13:04:20,FALSE,TRUE,35,FALSE,35,
22/06/2017 13:04:35,FALSE,TRUE,55,FALSE,55,
22/06/2017 13:04:37,FALSE,TRUE,100,FALSE,55,
22/06/2017 13:04:37,FALSE,TRUE,38,FALSE,55,
22/06/2017 13:04:38,FALSE,FALSE,35,FALSE,55,
22/06/2017 13:04:39,FALSE,FALSE,35,FALSE,38,
22/06/2017 13:04:40,FALSE,FALSE,35,TRUE,38,
22/06/2017 13:04:41,FALSE,FALSE,55,TRUE,38,
22/06/2017 13:04:42,FALSE,FALSE,55,TRUE,55,
22/06/2017 13:04:44,FALSE,FALSE,38,TRUE,55,
22/06/2017 13:04:45,TRUE,FALSE,38,TRUE,40,
22/06/2017 13:04:46,TRUE,FALSE,38,TRUE,40,
22/06/2017 13:04:48,TRUE,FALSE,55,FALSE,40,
22/06/2017 13:04:49,TRUE,FALSE,55,FALSE,25,
22/06/2017 13:04:50,TRUE,FALSE,55,FALSE,45,
22/06/2017 13:04:52,FALSE,TRUE,55,FALSE,60,
22/06/2017 13:04:53,FALSE,TRUE,40,FALSE,80,
22/06/2017 13:04:54,FALSE,TRUE,40,FALSE,80,
22/06/2017 13:04:56,FALSE,TRUE,40,FALSE,75,
22/06/2017 13:04:57,FALSE,TRUE,40,FALSE,90,
22/06/2017 13:04:58,FALSE,TRUE,55,FALSE,88,
22/06/2017 13:05:00,TRUE,TRUE,55,TRUE,23,
22/06/2017 13:05:01,TRUE,TRUE,55,TRUE,45,
22/06/2017 13:05:02,TRUE,TRUE,20,TRUE,78,
22/06/2017 13:05:04,TRUE,TRUE,45,TRUE,45,
22/06/2017 13:05:05,TRUE,TRUE,85,TRUE,69,
22/06/2017 13:05:06,TRUE,TRUE,62,TRUE,45,
22/06/2017 13:05:08,TRUE,TRUE,100,TRUE,35,

这是我的按钮处理程序代码:

  using System.Globalization;

  public static string dateFormatString = "dd/MM/yyyy HH:mm:ss";


  private void btnSeperateFile_Click(object sender, EventArgs e)
        {
            int discharge_counter = 0;
            int charge_counter = 0;

            if (tbOutputFilePath2 != null)
            {

                List<DrawNew> ObservingData = new List<DrawNew>(); // List to store all available DrawNew objects from the CSV

                // Loops through each lines in the CSV
                foreach (string line in System.IO.File.ReadAllLines(outputFilePath.Text).Skip(1)) // .Skip(1) is for skipping header
                {

                    string[] valuesCsvLine = line.Split(',');
                    DrawNew mngInstance = new DrawNew();

                    mngInstance.Date = DateTime.ParseExact(valuesCsvLine[0], dateFormatString, CultureInfo.InvariantCulture); 
                    mngInstance.Pass = (valuesCsvLine[1] == "TRUE" ? true : false);
                    mngInstance.CutMark1 = (valuesCsvLine[2] == "TRUE" ?true: false);
                    mngInstance.Marks1 = int.Parse(valuesCsvLine[3]);                 
                    mngInstance.CutMark2 = (valuesCsvLine[4] == "TRUE" ? true : false);
                    mngInstance.Marks2 = int.Parse(valuesCsvLine[5]); 

                    ObserveData.Add(mngInstance);

                    if (mngInstance.Pass==true && mngInstance.CutMark1==true)
                    {


                    }
                    if (mngInstance.Pass == false && mngInstance.CutMark1 == true)
                    {

                    }
                    if (mngInstance.Pass == true && mngInstance.CutMark2 == true)
                    {

                    }
                    if (mngInstance.Pass == false && mngInstance.CutMark2 == true )
                    {

                    }            
                }
            }

这是用于存储.csv文件的数据的类:

 class DrawNew
    {
        public DateTime Date { get; set; } // property to store Date 
        public bool Pass { get; set; } // property to store ChargeMode
        public bool CutMark1 { get; set; } // property to store ChargeMode
        public int Marks1 { get; set; } // property to store ScheduleStep
        public bool CutMark2 { get; set; } // property to store ChargeMode
        public int Marks2 { get; set; } // property to store ScheduleStep 
     }

如果你能纠正我的代码作为我的条件。我想创建新的.csv文件作为我的条件。我想问我如何分开&#34; Pass&#34;列中的时间在我的示例中为FALSE-TRUE-FALSE-TRUE并希望保存为上面给出的不同名称。请帮我解决这个问题。如果我能非常感激。

0 个答案:

没有答案