制表符分隔记录的 CsvHelper 解析失败

CsvHelper parsing fails for tab delimitted record

我有一个制表符分隔的文件并使用 CsvHelper 在 .Net c# 中解析它 不知何故它在 1 条记录处失败并抛出 BadData,记录中似乎没有错,我尝试粘贴 excel 并使用文本到列,它工作正常。

重现错误的代码

//add CsvHelper nuget package to project
//Create .txt file with below line and pass path of file into function
//copy text from https://gist.github.com/JitenPatoliya/9f9a15eb388c32f46231aa9fa35dd6e3
//Paste into your text file and try this code

private void TestCSvParser(string fullFilePath)
        {
            try
            {
                string ext = Path.GetExtension(fullFilePath);
                using var reader = new StreamReader(fullFilePath);
                using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
                csv.Configuration.BadDataFound = BadDataFound;
                csv.Configuration.DetectColumnCountChanges = true;
                if (ext.IsContains(".txt"))
                {
                    csv.Configuration.Delimiter = "\t";
                }
                while (csv.Read())
                {
                    var fullrow = csv.Context.RawRecord;
                    var record = csv.Parser.Read();
                }
            }
            catch (Exception ex)
            {

            }
        }

private void BadDataFound(ReadingContext ctx)
        {
         //put debug point here
        }

感谢并感谢您花时间解决这个问题

好的,问题是您的其中一个字段包含一个 ",这是一个引号字符。您要么必须转义它,要么忽略文件中的引号。在下面的代码中,我忽略了。此外,如果您将使用 Parser.Read,则不应使用 csv.Read,因为它在读取之前已经前进到下一条记录。因此,您的代码变为:

private void TestCSvParser(string fullFilePath)
{    
    try
    {
        string ext = Path.GetExtension(fullFilePath);
        using var reader = new StreamReader(fullFilePath);
        using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
        csv.Configuration.BadDataFound = BadDataFound;
        csv.Configuration.DetectColumnCountChanges = true;
        if (ext.Contains(".txt"))
        {
            csv.Configuration.Delimiter = "\t";                    
            csv.Configuration.IgnoreQuotes = true;
        }
        string[] record;
        while((record = csv.Parser.Read()) != null)
        {
            var fullrow = csv.Context.RawRecord;
        }                
    }
    catch (Exception ex)
    {

    }
}