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.

4 Answers 4

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]
   });
  }
 }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.