0

Suppose there is a data.csv file and it looks like this :

    Ann     300     700     1000    

    Zoe     2       4       6   

    Ian     100     200     300 

    Zak     90      90      180

I need to grab values for each cell and store them in a list so that when I print them it looks like so:

list.ForEach(Console.Write);

Ann 300 700 1000 Zoe 2 4 6 Ian 100 200 300 Zak 90 90 180

I tried looking for answers but I am struggling to read in data and tokenize each cell element in the csv.

  • 1
    Possible duplicate of [Reading CSV file and storing values into an array](http://stackoverflow.com/questions/5282999/reading-csv-file-and-storing-values-into-an-array) – deostroll Nov 04 '15 at 03:06
  • Don't load whole file into memory (list or array) unless you know it's always small. Otherwise, prefer stream. – qxg Nov 04 '15 at 03:27
  • i am using only 200 entries - do you think that is ok? –  Nov 04 '15 at 03:31

1 Answers1

3

Try this

public void ReadCSV()
{
    var reader = new StreamReader(File.OpenRead(@"C:\data.csv"));    
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        string[] values = line.Split('your separator');

        foreach(string item in values)
        {
            Console.WriteLine(item);
        }
    }
}
Gagan Jaura
  • 710
  • 1
  • 5
  • 14