我有.csv
文件,文件样本就是这样,
Date,Pass,CutMark1,Marks1,CutMark2,Marks2
22/06/2017 13:04:18,FALSE,TRUE,TRUE,FALSE,FALSE
22/06/2017 13:04:20,FALSE,TRUE,TRUE,FALSE,FALSE
22/06/2017 13:04:35,FALSE,TRUE,TRUE,FALSE,FALSE
22/06/2017 13:04:37,FALSE,TRUE,TRUE,FALSE,FALSE
22/06/2017 13:04:37,FALSE,TRUE,TRUE,FALSE,FALSE
22/06/2017 13:04:38,FALSE,FALSE,TRUE,FALSE,FALSE
22/06/2017 13:04:39,FALSE,FALSE,TRUE,FALSE,FALSE
22/06/2017 13:04:40,FALSE,FALSE,TRUE,TRUE,FALSE
22/06/2017 13:04:41,FALSE,FALSE,TRUE,TRUE,TRUE
22/06/2017 13:04:42,FALSE,FALSE,FALSE,TRUE,TRUE
22/06/2017 13:04:44,FALSE,FALSE,FALSE,TRUE,TRUE
22/06/2017 13:04:45,TRUE,FALSE,FALSE,TRUE,TRUE
22/06/2017 13:04:46,TRUE,FALSE,FALSE,TRUE,TRUE
22/06/2017 13:04:48,TRUE,FALSE,FALSE,FALSE,TRUE
22/06/2017 13:04:49,TRUE,FALSE,FALSE,FALSE,TRUE
22/06/2017 13:04:50,TRUE,FALSE,FALSE,FALSE,TRUE
22/06/2017 13:04:52,FALSE,TRUE,TRUE,FALSE,TRUE
22/06/2017 13:04:53,FALSE,TRUE,TRUE,FALSE,TRUE
22/06/2017 13:04:54,FALSE,TRUE,TRUE,FALSE,TRUE
22/06/2017 13:04:56,FALSE,TRUE,TRUE,FALSE,TRUE
22/06/2017 13:04:57,FALSE,TRUE,TRUE,FALSE,TRUE
22/06/2017 13:04:58,FALSE,TRUE,TRUE,FALSE,TRUE
22/06/2017 13:05:00,TRUE,TRUE,TRUE,TRUE,FALSE
22/06/2017 13:05:01,TRUE,TRUE,TRUE,TRUE,FALSE
22/06/2017 13:05:02,TRUE,TRUE,TRUE,TRUE,FALSE
22/06/2017 13:05:04,TRUE,TRUE,TRUE,TRUE,FALSE
22/06/2017 13:05:05,TRUE,TRUE,TRUE,TRUE,FALSE
22/06/2017 13:05:06,TRUE,TRUE,TRUE,TRUE,FALSE
22/06/2017 13:05:08,TRUE,TRUE,TRUE,TRUE,FALSE
所以现在我想保存这个文件如下;在这里Pass column
数据流是这样的,FALSE> TRUE> FALSE> TRUE ..这是我的.csv文件的样本。但还有更多的原始文件。在原始文件中。在这里我想分离这个原始的.csv文件,如下所示;
首先遇到False所以所有False详细信息并检查Marks1 = TRUE然后保存Discharge1_Mark1.csv
,并查看Marks2列,如果该列为true,则保存Discharge1_Mark2.csv
。
其次Pass列遇到True然后检查Marks1 = TRUE然后保存Charge1_Mark1.csv
,然后如果Marks2 = true则保存Charge1_Mark2.csv
。
NOw再次传递列符合False然后想要保存文件Discharge2_Marks1.csv
。
就像我想要保存我的文件一样。
我想在这里说相应的Pass列TRUE,False你想保存文件就像我之前提到的那样,TRUE,FALSE meet.1,2,3,...你要检查相应的传递列TRUE相应的Marks1,Marks2列为false。 在这里,我想保存我生成的文件各自的列。
如果我保存Marks1,那么列标题应该是(Date,Pass,CutMarks1,Marks1) 如果我保存Marks2,那么列标题应该是(Date,Pass,CutMarks2,Marks2)
请在我的代码中提供我之前提到的生成文件的方法。我的代码如下。
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 = (valuesCsvLine[3] == "TRUE" ?true: false);
mngInstance.CutMark2 = (valuesCsvLine[4] == "TRUE" ? true : false);
mngInstance.Marks2 = (valuesCsvLine[5] == "TRUE" ?true: false);
ObserveData.Add(mngInstance);
if (mngInstance.Pass==true && mngInstance.Marks1==true)
{
}
if (mngInstance.Pass == false && mngInstance.Marks1 == true)
{
}
if (mngInstance.Pass == true && mngInstance.Marks2 == true)
{
}
if (mngInstance.Pass == false && mngInstance.Marks2 == 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 bool Marks1 { get; set; } // property to store ScheduleStep
public bool CutMark2 { get; set; } // property to store ChargeMode
public bool Marks2 { get; set; } // property to store ScheduleStep
}
我的问题是我如何生成这些文件,如前所述。请帮助解决我的问题。
答案 0 :(得分:1)
对于每种类型的输出csv文件,您将需要4 lists
,如下所示。根据每个DrawNew
对象的if条件,将其添加到自己的列表中。
List<DrawNew> dischargeMark1List
List<DrawNew> dischargeMark2List
List<DrawNew> charge1Mark1List
List<DrawNew> charge1Mark2List
GenerateOutputCsv(List<DrawNew> selectedData)
以将数据写入正确的文件名。您只需要像在btnSeperateFile_Click(..)
方法中那样进行相同的检查。 答案 1 :(得分:1)
我写了我的草案想法如下。我只关注4个案例:
我认为可能还有另一种情况。
首先,我创建了一个新模型来存储数据。
public class Charge
{
public DateTime Date { get; set; }
public bool Pass { get; set; }
public bool CutMark { get; set; }
public int Marks { get; set; }
}
然后,我创建了4个列表来存储4个案例的数据:
var discharge1Mark1 = new List<Charge>();
var discharge1Mark2 = new List<Charge>();
var charge1Mark1 = new List<Charge>();
var charge1Mark2 = new List<Charge>();
在你的for循环中,我将按如下方式处理:
var date = DateTime.ParseExact(valuesCsvLine[0], dateFormatString, CultureInfo.InvariantCulture);
var pass = (valuesCsvLine[1] == "TRUE" ? true : false);
var cutMark1 = (valuesCsvLine[2] == "TRUE" ? true : false);
var marks1 = int.Parse(valuesCsvLine[3]);
var cutMark2 = (valuesCsvLine[4] == "TRUE" ? true : false);
var marks2 = int.Parse(valuesCsvLine[5]);
if (pass)
{
if (cutMark1)
discharge1Mark1.Add(new Charge
{
Date = date,
Pass = pass,
Marks = marks1,
CutMark = cutMark1
});
else
discharge1Mark2.Add(new Charge
{
Date = date,
Pass = pass,
Marks = marks2,
CutMark = cutMark2
});
}else
{
if (cutMark1)
charge1Mark1.Add(new Charge
{
Date = date,
Pass = pass,
Marks = marks1,
CutMark = cutMark1
});
else
charge1Mark2.Add(new Charge
{
Date = date,
Pass = pass,
Marks = marks2,
CutMark = cutMark2
});
}
要为CSV编程创建标题,您只需执行以下操作:
private void ExportToCSV(List<Charge> data, bool discharge, int num)
{
var filename = discharge ? "Discharge1" : "Charge1";
filename += (num == 1) ? "_Mark1.csv" : "_Mark2.csv";
var headerCol1 = "Date";
var headerCol2 = "Pass";
var headerCol3 = (num == 1) ? "CutMark1" : "CutMark2";
var headerCol4 = (num == 1) ? "Mark1" : "Mark2";
//your exporting function below
}
希望你明白,我的想法可以帮助你。
答案 2 :(得分:0)
如果我理解你的问题,我想建议如下:
在DrawNew中,我将添加一个新字段来存储FileName,该记录应该导出到哪个csv文件。
public 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 bool Marks1 { get; set; } // property to store ScheduleStep
public bool CutMark2 { get; set; } // property to store ChargeMode
public bool Marks2 { get; set; } // property to store ScheduleStep
public string FileName { get; set; }
}
然后,在for循环中,我使用一些参数来确定每条记录的文件名。
List<DrawNew> ObservingData = new List<DrawNew>(); // List to store all available DrawNew objects from the CSV
// Loops through each lines in the CSV
var fileName = "";
var previousPassValue = true;
var count = 0;
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 = (valuesCsvLine[3] == "TRUE" ? true : false);
mngInstance.CutMark2 = (valuesCsvLine[4] == "TRUE" ? true : false);
mngInstance.Marks2 = (valuesCsvLine[5] == "TRUE" ? true : false);
if (count == 0)
{
previousPassValue = mngInstance.Pass;
count++;
}
if (previousPassValue != mngInstance.Pass)
{
previousPassValue = mngInstance.Pass;
count++;
}
if (mngInstance.Pass == true && mngInstance.Marks1 == true)
{
fileName = "Charge" + count + "_Mark1.csv";
}
if (mngInstance.Pass == false && mngInstance.Marks1 == true)
{
fileName = "DisCharge" + count + "_Mark1.csv";
}
if (mngInstance.Pass == true && mngInstance.Marks2 == true)
{
fileName = "Charge" + count + "_Mark2.csv";
}
if (mngInstance.Pass == false && mngInstance.Marks2 == true)
{
fileName = "DisCharge" + count + "_Mark2.csv";
}
mngInstance.FileName = fileName;
ObservingData.Add(mngInstance);
}
我认为您的数据文件是正确的。在将其导出为excel之前,您将按FileName对ObservingData列表进行排序。
希望这有帮助。