Kingspod
Kingspod

Reputation: 49

Using CSVHelper to parse a pipe deliminated file with quotes in the content

I have an externally generated pipe deliminated file which I am trying to parse using .NET CSVHelper and enter into a SQL database but am finding a problem. The main issue is that there's quotes in the content but the delimiter is pipe and the quotes aren't escaped.

Imagine this being the file

ID|Price|Description|stock

133|55.89|Standard Electric Drill|23

134|3.40|3.5" Drill Bit|56

I've set the deliminater to pipe but it errors when it reaches a record with the quote ( in this case ID 134 with the quote " denoting inch) in the description. The error suggests setting BadDataFound to Null but that would just ignore the record?

Is there any configuration that I've missed that would allow this? Or is the best way simply to replace quotes with {quote} or something, parse it and then replace back before entering it into the database?

Upvotes: 1

Views: 1468

Answers (1)

David Specht
David Specht

Reputation: 9074

The NoEscape mode should work as long as no field contains a pipe or newline character.

void Main()
{
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        Mode = CsvMode.NoEscape,
        Delimiter = "|"
    };
    
    using (var reader = new StringReader("ID|Price|Description|stock\n133|55.89|Standard Electric Drill|23\n134|3.40|3.5\" Drill Bit|56"))
    using (var csv = new CsvReader(reader, config))
    {
        var records = csv.GetRecords<Foo>().Dump();
    }
}

public class Foo
{
    public int ID { get; set; }
    public string Price { get; set; }
    public string Description { get; set; }
    public int stock { get; set; }
}

Upvotes: 5

Related Questions