JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.74k stars 1.06k forks source link

Multiple Commas Inside Value When File Delimiter Is A Comma Not Working #2198

Open sofimarchesini opened 12 months ago

sofimarchesini commented 12 months ago

When having a file delimiter as a comma, and having a value that has more than one comma inside such as : "Dopel, France, Paris" does not takes the complete value, instead it takes Dopel as one value etc. The format of the csv is correct. Thank you

AltruCoder commented 12 months ago

Can you provide an example of code that is not working for you. This works for me and gives me the expected result of Foo.Name = "Dopel, France, Paris"

void Main()
{
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        Delimiter = ","
    };
    var reader = new StringReader("Id,Name\n1,\"Dopel, France, Paris\"");
    var csv = new CsvReader(reader, config);
    var records = csv.GetRecords<Foo>().ToList();
}

// You can define other methods, fields, classes and namespaces here
public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}
sofimarchesini commented 12 months ago

Oh, my exact example is this : My row : 23, 238, 239057.41,"France Paris Spain, Idaho, AAA",1.777, FIXED And first it brings AAA",1.777 as a value and then "France Paris Spain" as another value.

And part of my code is ( it is more complex than this)


void Main()
{
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        Delimiter = ",", 
                IgnoreBlankLines = true, 
                Mode = CsvMode.Escape
    };
    var reader = new StringReader("23, 238, 239057.41,"France Paris Spain, Idaho, AAA",1.777, FIXED");
    var csv = new CsvReader(reader, config);
    var records = csv.GetRecords<Foo>().ToList();
}
AltruCoder commented 12 months ago

It looks like you are using CsvMode.Escape. There isn't a lot of documentation on exactly what that does, but I believe it was created to help out with those situations where you have an un-escaped double quote in the middle of a field. dbc on Stackoverflow gives some indication on what it does in his answer to a question here.

  1. Set Mode = CsvMode.Escape to disable wrapping of fields in quotes, and set Escape to some other character such as \ or \t that you do not expect to encounter in the file in practice:
    var cfg = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
    {
    Mode = CsvMode.Escape,
    Escape = '\\',
    // Remainder unchanged.
    }

    Even if you do this, delimiters, escape and newline characters inside CSV fields must still be properly escaped using the selected escape character.

If you want it to work using CsvMode.Escape, you would need to escape your delimiters inside a CSV field. In other words, " no longer works to indicate that anything inside a set of " should be treated as being inside a field and not as a delimiter. Your example would need to look more like

void Main()
{
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        Delimiter = ",",
        IgnoreBlankLines = true,
        Mode = CsvMode.Escape,
        Escape = '\\'
    };
    var reader = new StringReader("23, 238, 239057.41,France Paris Spain\\, Idaho\\, AAA,1.777, FIXED");
    var csv = new CsvReader(reader, config);
    var records = csv.GetRecords<Foo>().ToList();
}

Unless you really need CsvMode.Escape, I would stick with the default of CsvMode.RFC4180.

sofimarchesini commented 12 months ago

That worked for me thank you!