我有一个包含字符串数据的 csv 文件,如果“作业”列为空,我想过滤掉所有数据行,创建一个新的 csv 文件并将剩余的数据行写入其中。
字符串数据包含双引号"",第一行的"Id"、"Name"、"Job"实际上是字符串数据,而不是列,这意味着如果数据行的第三个字符串数据是双引号"" ,它被过滤掉了。
"Id" , "Name" , "Job"
"1" , "Alan" , "Engineer"
"2" , "Bob" , "Technician"
"3" , "Charlie" , ""
"4" , "Danny" , ""
根据上面的csv的剩余数据预计为
"Id" , "Name" , "Job"
"1" , "Alan" , "Engineer"
"2" , "Bob" , "Technician"
如果你有 csv helper 工具,你可以使用工具解析文件到 List 或 DataTable,过滤数据和导出。
如果你没有,你可以试试:
using (var reader = new StreamReader(sourcePath))
{
using (var writer = new StreamWriter(destiPath))
{
String line;
while ((line = reader.ReadLine()) != null)
{
var list = line.Split(',');
if (list.Length > 2 && !string.IsNullOrEmpty(list[2].Trim(' ','\"')))
{
writer.WriteLine(line);
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句