JoshClose / CsvHelper

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

Parser treats quoted string containing a comma as two separate records #2194

Closed jchristn closed 10 months ago

jchristn commented 10 months ago

Describe the bug CsvParser is treating a quoted row that includes a comma as two separate records.

To Reproduce

using System;
using System.IO;
using System.Text;
using CsvHelper.Configuration;
using System.Globalization;

namespace Sandbox
{
    public partial class Program
    {
        static void Main(string[] args)
        {
            CsvConfiguration config = new CsvConfiguration(CultureInfo.InvariantCulture);
            config.BadDataFound = null;

            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(Encoding.UTF8.GetBytes(FileContents));
                ms.Seek(0, SeekOrigin.Begin);

                using (TextReader tr = new StreamReader(ms))
                {
                    using (CsvHelper.CsvParser cp = new CsvHelper.CsvParser(tr, config))
                    {
                        while (cp.Read())
                        {
                            if (cp.Record != null && cp.Record.Length > 0)
                            {
                                foreach (string val in cp.Record) Console.WriteLine(val);
                            }
                        }
                    }
                }
            }
        }

        static string FileContents =
            "\"Year\", \"Score\", \"Title\"" + Environment.NewLine +
            "1968,  86, \"Greetings\"" + Environment.NewLine +
            "1970,  17, \"Bloody Mama\"" + Environment.NewLine +
            "1970,  73, \"Hi, Mom!\"" + Environment.NewLine +
            "1971,  40, \"Born to Win\"" + Environment.NewLine +
            "1973,  98, \"Mean Streets\"" + Environment.NewLine +
            "1973,  88, \"Bang the Drum Slowly\"" + Environment.NewLine +
            "1974,  97, \"The Godfather, Part II\"" + Environment.NewLine +
            "1976,  41, \"The Last Tycoon\"" + Environment.NewLine +
            "1976,  99, \"Taxi Driver\"" + Environment.NewLine +
            "1977,  47, \"1900\"" + Environment.NewLine +
            "1977,  67, \"New York, New York\"" + Environment.NewLine +
            "1978,  93, \"The Deer Hunter\"" + Environment.NewLine +
            "1980,  97, \"Raging Bull\"" + Environment.NewLine;
    }
}

Expected behavior "Hi and Mom!" would appear on the same line "The Godfather and Part II" would appear on the same line "New York and New York" would appear on the same line

Screenshots N/A

Additional context N/A

JoshClose commented 10 months ago

You're getting a BadDataException but you're ignoring it.

A CSV field that is quoted shouldn't have a space before the quote.

If you set TrimOptions = TrimOptions.Trim in the config, it'll work.

jchristn commented 10 months ago

Thanks @JoshClose for your fast response. I appreciate the time and effort you put into this amazing library!