Home > Net >  C#: Trying to copy CSV file into an array
C#: Trying to copy CSV file into an array

Time:07-10

I have this code to copy my csv into an array:

var filePath = @"C:\Users\sti\source\repos\AutoAppr\AutoAppr\bin\Debug\Spreadsheet.csv";
var data = File.ReadLines(filePath).Select(x => x.Split(',')).ToArray();

This works, however my csv has strings separated with quotes/commas like: "red","green","blue"

Some of the fields have commas in them so it breaks them up.

I tried editing x.Split(',') into x.Split('\",\"') however this only seems to accept characters. Is there a fix for this?

Thanks

CodePudding user response:

This worked perfectly thanks to Andrew:

string[] stringSeparators = new string[] { "\",\"" };
var filePath = @"C:\Users\sti\source\repos\AutoAppr\AutoAppr\bin\Debug\Spreadsheet.csv";
var data = File.ReadLines(filePath).Select(x => x.Split(stringSeparators, StringSplitOptions.None)).ToArray();

CodePudding user response:

You can try this one CsvHelper.

  • Related