Home > Back-end >  CsvHelper - How to config the setting about quote?
CsvHelper - How to config the setting about quote?

Time:03-16

When the spec data has quote ("Jake"), I got an error message below.

"CsvHelper.BadDataException: You can ignore bad data by setting BadDataFound to null."

It will work when I remove log's quote (Jake).

So the question is how to config the setting about quote?

Notice the log has a col with no quote.

CSV

Id,Name,Type
1, "Jake",User

MAP

public LogProfile()
{
    Map(m => m.Id).Name("Id");
    Map(m => m.Name).Name("Name");
    Map(m => m.Type).Name("Type");
}

Convert

const string headerPrefix = "Id,";
var path = @"D:\test.csv";
var readConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
    ShouldQuote = x => x.Field.Contains("\"")
};
var input = new List<LogRecord>();
using (var reader = new StreamReader(path))
using (var csv = new CsvReader(reader, readConfiguration))
{
    csv.Context.RegisterClassMap<LogProfile>();
    var isHeader = true;
    while (csv.Read())
    {
        if (isHeader)
        {
            
            if(csv.Parser.RawRecord.Contains(headerPrefix))
            {
                csv.ReadHeader();
                isHeader = false;
            }
            continue;
        }
        input.Add(csv.GetRecord<LogRecord>());
    }
}
Logger.LogInformation($"Done {path}");

CodePudding user response:

Try it with the Configuration setting of TrimOptions.Trim.

void Main()
{
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        TrimOptions = TrimOptions.Trim
    };
    
    using (var reader = new StringReader("Id,Name,Type\n1, \"Jake\",User"))
    using (var csv = new CsvReader(reader, config))
    {
        var records = csv.GetRecords<LogRecord>();
    }
}

public class LogRecord
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
}
  • Related