I’m reading a csv file using c# here is a little code snippet.
using (StreamReader readFile = new StreamReader("C:\\temp\\" + whichTable))
{
while ((line = readFile.ReadLine()) != null)
{
row = line.Split(',');
switch (row.Length)
{
case 5:
if (counter == 0)
{
break;
}
else
{
v00.Add(Convert.ToInt32(Regex.Replace(row[0], @"[^\w\.@-]", "")));
}
if (row[1] == "")
{
v01.Add((1));
}
else
{
v01.Add(Convert.ToInt32(Regex.Replace(row[1], @"[^\w\.@-]", "")));
}
if(row[2]=="")
{
v02.Add(2);
}
else
{
v02.Add(Convert.ToInt32(Regex.Replace(row[2], @"[^\w\.@-]", "")));
}
v3.Add(row[4]);
v4.Add(row[3]);
counter++;
break;
}
counter++;
}
break;
}
as you can tell from my code i test the length of the string row to make sure that its five long exactly. My problem is that if have a field within the csv with a comma it then calculates to more then 5. My csv is well formed so when that happens i do have a that field double quoted. How can i tell c# only count the commas outside of double quotes? Thats really my question.
Don’t parse CSV yourself – the format is more difficult to parse properly than most people realize. There are many exsisting good CSV parsers that you can use instead.
There is the
TextFieldParserlibrary that lives in the Microsoft.VisualBasic.FileIO namespace (regular .NET libarary), and many third party ones – FileHelpers is a popular free choice.