我想阅读CSV
文件,带标题的选定列将保存在另一个新的CSV中。
*我想为我的代码添加以下要求。*请帮我添加这些代码。
在这里我考虑这个要求;
Age >=25 && Marks0 ==True
以此名称Pass.csv
保存此.csv文件中的标题列(Index,Age,Marks0)
其他要求是Age >=20 && Age <=25 & Marks1 ==False
以此名称General.csv
保存此.csv文件中的标题下列(Index,Age,Marks0,Marks1)
其他人保存在Ignore.csv
标题(Index,Age,Marks0,Marks1,Marks2,Marks3)
这是我的样本.CSV
文件
Index,Age,Marks0,Marks 1,Marks2,Marks3
1,22,TRUE,FALSE,FALSE,FALSE
2,30,TRUE,TRUE,FALSE,FALSE
3,19,TRUE,TRUE,FALSE,FALSE
4,25,TRUE,TRUE,FALSE,FALSE
5,22,TRUE,TRUE,FALSE,FALSE
6,26,TRUE,TRUE,FALSE,FALSE
7,27,TRUE,FALSE,FALSE,TRUE
8,29,TRUE,FALSE,FALSE,TRUE
9,25,TRUE,FALSE,FALSE,TRUE
10,22,TRUE,FALSE,FALSE,TRUE
11,23,TRUE,FALSE,FALSE,TRUE
我创建了一个类来存储我的列值
class Marks
{
public int Index { get; set; } // property to store Index
public int Age { get; set; } // property to store Age
public bool Marks0 { get; set; } // property to store Marks0
public bool Marks01 { get; set; } // property to store Marks01
public bool Marks2 { get; set; } // property to store Marks2
public bool Marks3 { get; set; } // property to store Marks3
}
阅读.CSV
文件的代码如下;
private void button1_Click(object sender, EventArgs e)
{
List<Marks> Observing1 = new List<Marks>(); // List to store all available Marks objects from the CSV
Marks statusInt = new Marks();
// 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
{
// here line stands for each line in the CSV file
string[] InCsvLine = line.Split(',');
statusInt.Index=int.Parse(InCsvLine[0]);
statusInt.Age=int.Parse(InCsvLine[1]);
statusInt.Mark0 = (InCsvLine[2] == "TRUE" ? true : false);
statusInt.Mark1 = (InCsvLine[3] == "TRUE" ? true : false);
statusInt.Mark2 = (InCsvLine[4] == "TRUE" ? true : false);
statusInt.Mark3 = (InCsvLine[5] == "TRUE" ? true : false);
}
}
请为解决我的问题提供任何帮助。
答案 0 :(得分:1)
我发现了多个问题,但为了帮助您入门,您可以像这样从csv中分隔列:
var recordsToSaveInPassFile = new List<Class>();
var recordsToSaveInGeneralFile = new List<Class>();
var lines = File.ReadAllLines(@"C:\temp\data.csv", Encoding.GetEncoding(1252));
foreach (var line in lines)
{
var items = line.Split(';');
int parsedAge;
if (int.TryParse(items[0], out parsedAge))
{
if(parsedAge > 24)
{
var recordToPass = new Class
{
Age = items[0],
item2 = items[1]
};
recordsToSaveInPassFile.Add(recordToPass);
}
else if(parsedAge > 19)
{
var recordToGeneral = new Class
{
Age = items[0],
item2 = items[1]
};
recordsToSaveInGeneralFile.Add(recordToGeneral);
}
continue;
}
Console.WriteLine($@"'{line}': could not be parsed");
}
这可以做得更好,但这可能会让您了解如何从csv文件中检查不同的值。
答案 1 :(得分:0)
我在评论中谈到的是:
List<Marks> Observing1 = new List<Marks>(); // List to store all available Marks objects from the CSV
// Marks statusInt = new Marks(); <<<<<< Move into loop!
// 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
{
var statusInt = new Marks();
// here line stands for each line in the CSV file
string[] InCsvLine = line.Split(',');
statusInt.Index=int.Parse(valuesInCsvLine[0]);
statusInt.Age=int.Parse(valuesInCsvLine[1]);
statusInt.Mark0 = (InCsvLine[2] == "TRUE" ? true : false);
statusInt.Mark1 = (InCsvLine[3] == "TRUE" ? true : false);
statusInt.Mark2 = (InCsvLine[4] == "TRUE" ? true : false);
statusInt.Mark3 = (InCsvLine[5] == "TRUE" ? true : false);
// Add your result to the List!
Observing1.Add(stsusInt);
}
// Now you can filter as you wish
var filteredByAge = Observing1.Where( x => x.Age > 25 ).ToList();
// Writing to a new File is left as assignment for OP