I am trying to parse a CSV file with commas in the data. All fields do not have quotes around them, but SOME of the dollar amounts do when there is a comma in them. So for example the file looks something like this:
0000-0C,Sales-Data Entry,10/31/2016,000000,VJ,Comapny,,312.3
0000-0C,Sales-Data Entry,10/31/2016,000000,VJ,Company,,"1,420.97"
So as you can see there is a comma in the 1,420.97 but it does have quotes around this. Is there a way to reliably read this file?
Here is my code:
var path = @"glid.csv";
TextFieldParser parser = new TextFieldParser(path);
parser.HasFieldsEnclosedInQuotes = true;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
parser.ReadLine();
fields = parser.ReadFields();
//do something
}
parser.Close();
Are you sure you want to use both
and
As per the documentation https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(v=vs.110).aspx this will skip a line, process the next line over and over until the end of the file.
You may also wish to check your encoding type, TextFieldParser constructor accepts an Encoding type: https://msdn.microsoft.com/en-us/library/ms128085(v=vs.110).aspx
try the following encodings: https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx