DevyDev
DevyDev

Reputation: 886

CsvHelper dropping quotes

I have huge .csv files which ones I wanna put into list exactly as they appear in .csv file

Eample .csv file row would be

1141825007047,2019-02-18,02:55,"US","ATL","LAX","0","IAD","ORD","UA","UA236,UA208","UA2291,UA1155",2019-03-24 14:25,2019-03-24 22:20,2019-04-14 22:45,2019-04-15 08:54

When I use CsvHelper and set parameters like:

csv.Configuration.BadDataFound = null;
csv.Configuration.IgnoreQuotes = true;
csv.Configuration.Delimiter = ",";

Then values like "UA236,UA208" gets splitted into two columns, which is incorrect in my case.

If I set params as:

 csv.Configuration.BadDataFound = null;
 csv.Configuration.IgnoreQuotes = false;
 csv.Configuration.Delimiter = ",";

Then columns do get formatted correctly BUT it drops all the quoting. And it's dynamic thing regarding quotes, some columns will have it, some won't.

Desired output should be List of strings:

1141825007047
2019-02-18
02:55
"US"
"ATL"
"LAX"
"0"
"IAD"
"ORD"
"UA"
"UA236,UA208"
"UA2291,UA1155"
2019-03-24 14:25
2019-03-24 22:20
2019-04-14 22:45
2019-04-15 08:54

As requested, adding some code:

 using (StreamReader reader = new StreamReader(myStream))
                {
                    using (CsvReader csv = new CsvReader(reader))
                    {
                        csv.Configuration.BadDataFound = null;
                        csv.Configuration.IgnoreQuotes = false;
                        csv.Configuration.Delimiter = ",";

                        while (csv.Read())
                        {
                            List<string> values = new List<string>();
                            string line = string.Empty;

                            for (int i = 0; csv.TryGetField<string>(i, out string value); i++)
                            {
                                values.Add(value);
                            }
                        }
                    }
                }

Upvotes: 2

Views: 6065

Answers (1)

ElasticCode
ElasticCode

Reputation: 7875

You can get desired output by changing Delimiter to any character othere than , like \n with setting IgnoreQuotes to true

csv.Configuration.IgnoreQuotes = true;
csv.Configuration.Delimiter = "\\n";

Upvotes: 1

Related Questions