我正在尝试使用TextFieldParser导入 CSV 文件。一个特定的 CSV 文件由于其非标准格式而给我带来了问题。有问题的 CSV 的字段用双引号括起来。当特定字段中有一组额外的未转义双引号时,就会出现问题。
这是一个强调问题的过度简化的测试用例。我正在处理的实际 CSV 文件的格式并不完全相同,并且有几十个字段,其中任何一个都可能包含这些可能很棘手的格式问题。
TextReader reader = new StringReader("\"Row\",\"Test String\"\n" +
"\"1\",\"This is a test string. It is parsed correctly.\"\n" +
"\"2\",\"This is a test string with a comma, which is parsed correctly\"\n" +
"\"3\",\"This is a test string with double \"\"double quotes\"\". It is parsed correctly\"\n" +
"\"4\",\"This is a test string with 'single quotes'. It is parsed correctly\"\n" +
"5,This is a test string with fields that aren't enclosed in double quotes. It is parsed correctly.\n" +
"\"6\",\"This is a test string with single \"double quotes\". It can't be parsed.\"");
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.Delimiters = new[] { "," };
while (!parser.EndOfData)
{
string[] fields= parser.ReadFields();
Console.WriteLine("This line was parsed as:\n{0},{1}",
fields[0], fields[1]);
}
}
无论如何,是否可以使用 TextFieldParser 正确解析具有这种格式的 CSV?