Click here to Skip to main content
15,559,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am having trouble finding a forum for this issue. So I have to extract a few data within a csv file. Most of the data that I need are within double quotes but I am not sure as to how to read the strings.

I don't want to split them by ',' as some of the data I need to have ',' in them as well. What should I do or read on specifically to help my situation?

An example of the data that i have from the csv file is:

2,5910835-1H6S,"TRANSFORMER, AUDIO",,"T1, T2, T3",,3,N BND,
,,,,,,,,
3,5962-8670402PXH6S,"MICROCIRCUIT, LINEAR, PROGRAMMABLE PRIMA",,U26,,1,N BND,

The ones BOLD is the data I need extracted

What I have tried:

I have though of trying to read the characters after the double quotes but then I'm not sure how to end the reading after detecting the second double quote
Posted
Updated 9-Jun-22 4:54am

It is assumed that you are parsing the csv file by using your own logic.

I would like to recommend you an open source component like this A .NET library for reading and writing CSV files. Extremely fast, flexible, and easy to use. | CsvHelper[^]
 
Share this answer
 

When a separator character occurs inside a quoted string, the number of the double quotation characters is always an odd number. So step through a line one character at a time keeping a record of whether or not the double quote count is even or odd. If a separator character is found when the quoted count is odd, the current substring is a quoted substring. So wait until a separator is found when the quoted count is even before extracting that substring. I wrote the following many years ago as an exercise and you are welcome adapt it for your own use. But, as has already been suggested, you are better off using an existing library version than writing your own code.

C#
public class Reader
    {
        public List<List<string>> ReadCsv(string[] data, char separator)
        {
            var rows = new List<List<string>>();
            foreach (string text in data)
            {
                bool isquotedString = false;
                int startIndex = 0;
                int endIndex = 0;
                var cols = new List<string>();
                foreach (char c in text)
                {
                    //in a correctly formatted csv file,
                    //when a separator occurs inside a quoted string 
                    //the number of '"' is always an odd number
                    if (c == '"')
                    {
                        //toggle isquotedString
                        isquotedString = !isquotedString;
                    }
                        //ignore separator if embedded within a quoted string
                        if (c == separator && !isquotedString)
                        {
                            //this will add all cols except the last
                            cols.Add(trimQuotes(text.Substring(startIndex, endIndex - startIndex)));
                            startIndex = endIndex + 1;
                        }
                 
                    endIndex++;
                }
                //reached the last column so trim and add it
                cols.Add(trimQuotes(text.Substring(startIndex, endIndex - startIndex)));
                rows.Add(cols);
            }
            return rows;
        }
        private string trimQuotes(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return text;
            }
            return text[0] == '"' && text.Length > 2
                       //trim enclosing '"' and replace any embedded '""' with '"' 
                       ? text.Substring(1, text.Length - 2).Replace("\"\"", "\"")
                       //replace any embedded '""' with '"' 
                       : text.Replace("\"\"", "\"");
        }
    }

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900