【发布时间】:2018-08-26 07:48:26
【问题描述】:
下面是我的 CSV 结构(刚刚获取了标题行和第一个数据行。
Header 1,Header 2,Header 3,Header 4,Header5
Value 1,"Value2 a,Value 2b","Value3 a,Value 3b",Value 4,Value5
假设 CSV 有逗号分隔的分隔符,我可以读取 CSV、读取标题行和数据行。
一些代码片段 -
var fileContent = File.ReadAllLines(csvFile.FullName);
List<string> headerValues = null;
List<string> contentAllRows= null;
if (fileContent !=null && fileContent.Any())
{
headerValues = fileContent.First().Split(separators).ToList();
headerValues.ForEach(h => h = h.Trim());
contentAllRows = fileContent.Skip(1).ToList();
}
for (int row = 0; row <= contentAllRows.Count - 1; row++)
{
var column = contentAllRows[row].Split(separators).ToList();
}
上述代码片段的输出
headerValues[0] = "Header 1"
headerValues[1] = "Header 2"
headerValues[2] = "Header 3"
headerValues[3] = "Header 4"
headerValues[4] = "Header5"
contentAllRows ="Value 1,\"Value2 a,Value 2b\",\"Value3 a,Value 3b\",Value 4,Value5"
columns[0] = "Value 1"
columns[1] = "\"Value2 a"
columns[2] = "Value 2b\""
columns[3] = "\"Value3 a"
columns[4] = "Value 3b\""
columns[5] = "Value 4"
columns[6] = "Value5"
我的预期输出(针对上述每个标题值)-
columns[0]="Value 1"
columns[1]="Value2 a,Value 2b"
columns[2]="Value3 a,Value 3b"
columns[3]=""
columns[4]="Value5"
Split() 在上述情况下对我来说似乎是个问题。
对于上述场景,我们是否有一个简单的解决方案,我正在考虑在读取 CSV 时使用强类型对象。
上述场景是否适合 CSV 帮助模块@https://joshclose.github.io/CsvHelper/2.x/
任何建议表示赞赏。
【问题讨论】:
-
是的,我知道,您需要添加对
Microsoft.VisualBasic的引用。人们可以做更糟糕的事情:)。无论如何,我对其进行了测试,它确实可以按照您的预期解析字段。