0

My CSV file is formatted like this

Object,Value,Attribute
Object,Value,Attribute
Object,Value,Attribute

etc.

Would I need 3 separate arrays? (I think that's the best way of doing this..) One for object which I can then feed into my chart.. one for value and the attribute. I don't know much about arrays yet.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Andy
  • 373
  • 1
  • 7
  • 24

4 Answers4

1

You should create your own class that holds Object, Value and Attribute and store this in a list.

class SomeClass {
 public string MyObject { get; set; }
 public string MyValue { get; set; }
 public string MyAttribute { get; set; }
}

private List<SomeClass> myList = new List<SomeClass>();

public void ReadCsv(){
 using (var sr = new StreamReader("PathToCsvFile")) {
  string currentLine;

  while ((currentLine = sr.ReadLine()) != null) {
   var elements = currentLine.Split(',');

   myList.add(new SomeClass {
    MyObject = elements[0],
    MyValue = elements[1],
    MyAttribute = elements[2]
   });
  }
 }
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • String.Split may not be the best answer; won't work so well if any of the columns contain a comma that is not a delimiter. – John Wu Nov 05 '13 at 03:39
  • A comma as part of a value will always be a tricky situation in CSV files and should indeed be treated appropriately. That's beyond the scope of this question though. You've already provided a good link for that so OP should look there if he encounters this issue. – Jeroen Vannevel Nov 05 '13 at 03:46
0

I'd recommend one array of a class that holds object, value, and attribute, because then you don't need to worry about the complications of missing data, and changing all your arrays if you add more columns.

Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
0

What you do with the fields depends on how you're going to use the data. You probably need an array of structs or objects, where each element in the array is a row and each member of the object/struct is a column.

Here's a very simple example of a struct, where you can hold your data:

struct MyStruct
{
    string Column1;
    string Column2;
    //etc
}

And here's some code to populate it from the file:

List<MyStruct> rows = new List<MyStruct>;

s = reader.ReadLine();
while (s != null)
{
    string s[] columns = SplitLine(s); 
    MyStruct row = new MyStruct();
    row.Column1 = s[0];
    row.Column2 = s[1];
    rows.Add(row);
    s = reader.ReadLine();
}

Notice the ambiguous function "SplitLine." Lots of ways to split a string up . Look here for the best ways to split the string into fields.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

It really depends on what you want to do with the data. It seems you want to chart the data per category. If that is the case then the simplest way is to put each col in the same list.

1) read the csv file, per line 2) split the line based on comma 3) put data (in the same column) to the same list

John Ryann
  • 2,283
  • 11
  • 43
  • 60