Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a CSV with comma in data [duplicate]

Tags:

c#

.net

csv

Possible Duplicate:
Dealing with commas in a CSV file

I wrote myself a CSV parser it works fine until I hit this record: B002VECGTG,B002VECGTG,HAS_17131_spaceshooter,"4,426",0.04%,"4,832",0.03%,0%,1,0.02%,$20.47 ,1 The escaped , in "4,426" and in "4,426" brake my parser.

This is what I am using to parse the line of text:

            char[] comma = { ',' };
            string[] words = line.Split(comma);

How do I prevent my program from breaking?

like image 487
Joe Tyman Avatar asked Sep 19 '11 18:09

Joe Tyman


1 Answers

You can't just split on comma. To implement a proper parser for that case, you need to loop through the string yourself, keeping track of whether you are inside quotes or not. If you are inside a quoted string, you should keep on until you find another quote.

IEnumerable<string> LineSplitter(string line)
{
    int fieldStart = 0;
    for(int i = 0; i < line.Length; i++)
    {
        if(line[i] == ',')
        {    
            yield return line.SubString(fieldStart, i - fieldStart);
            fieldStart = i + 1;
        }
        if(line[i] == '"')
            for(i++; line[i] != '"'; i++) {}
    }
}
like image 147
Anders Abel Avatar answered Oct 06 '22 22:10

Anders Abel