1

I'm reading from csv file into a list in this way every row in the file is array cell and every cell is a array of columns . this is my reading cod :

public List<string[]> parseCSV(string path)
    {
       List<string[]> parsedData = new List<string[]>();
        string[] fields;

           TextFieldParser parser = new TextFieldParser(path);
            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");

            while (!parser.EndOfData)
            {
                fields = parser.ReadFields();
                parsedData.Add(fields);

            }

            parser.Close();

        return parsedData;
    }

In the end I got this list of string array . Now I want read specific value from specific cell and specific row from parsedData . how can I do that ?

My problem is that my "row" that I want is a string value , the value that im looking for is from column number 1 and row value of "key".

3
  • Have you thought about flattening the list of arrays of strings into a List of strings? Commented Jun 3, 2015 at 13:15
  • parseCSV("somePath")[0][0] Commented Jun 3, 2015 at 13:15
  • string Result = parsedData[RowIndex][ColIndex]; Commented Jun 3, 2015 at 13:16

3 Answers 3

2

So you have a List<string[]> but dont know how to access a specifc "row" or "column" from it? You just need to use the indexer. Arrays/lists are zero indexed:

string field3OfRow4 = parsedData[3][2]; // can cause an IndexOutOfRangeException

You have to handle the case that there are less rows or columns. Therefore use the Count property of the list and the Length property of the array:

if(parsedData.Count >= 4 && parsedData[3].Length >= 3)
{
    // safe
    string field3OfRow4 = parsedData[3][2];
}

Of couse you can also use a loop.

Sign up to request clarification or add additional context in comments.

4 Comments

Who's to say this isn't zero based?
@Curt: someone commented that this was field2 of row3 but that comment is now deleted. Just as my comment you are referring to.
My problem is that my "row" that i want is a string value , the value that im looking for is from column number 1 and row value of "key".
What means row value of "key"? Provide a little example.
1

You can access your List and array based on index like:

string specificValue = parsedData[row][col];

But a better option would be to create a class for your CSV item. Then parse each row and create an object of that class, It will give you much more flexibility.

List<MyCSVItem> list = new List<MyCSVItem>();

and then while parsing, you can pass the string[] to your class constructor and add the object to List<T>

while (!parser.EndOfData)
{
    string[] fields = parser.ReadFields();
    list.Add(new MyCSVItem(fields));
}

3 Comments

don't forget that i have the title row as my firs row
My problem is that my "row" that i want is a string value , the value that im looking for is from column number 1 and row value of "key".
@user2450821 See the edit in my answer, I added a block of code for this
0

Let's break this down:

  • We have a List<T> list and access an element (row) with T row = list[y-1] (zero-based)
  • In this case T stands for string[], and we access an element with: string field = row[x - 1]
  • To sum up, we can do this:

    List<string[]> rows = parseCSV("somePath");
    //-----------
    string[] fieldNames = rows[0]; //get first row
    string firstFieldname = fieldNames[0]; //get first row's first column
    //or shorter:
    string firstFieldname = rows[0][0];
    

  • As requested, you can search for the value "key" in column 1 in this way:

    for(int r = 0 /*change to 1 to skip first row*/ ; r < rows.Count; r++) {
        string[] entry = rows[r];
        string firstColumn = entry[0];
        if(firstColumn == "key") {
            Console.WriteLine("Found 'key' in first column of row " + r);
        }
    }
    

  • You can evaluate your CSV content like that:

    List<string[]> rows = parseCSV("somePath");
    for(int r = 1; r < rows.Count; r++) {
        string[] entry = rows[r];
        if(entry.Length != fieldNames.Length) {
            throw new FieldAccessException("illegal field length at row " + r + ": " + entry.Length);
        }
        Console.WriteLine("Row " + r + " = {");
        for(int f = 0; f < fieldNames.Length; f++) {
            Console.WriteLine("\t" + fieldNames[f] + " = " + entry[f]);
        }
        Console.WriteLine("}");
    }
    
  • 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.