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".
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.
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));
}
Let's break this down:
List<T> list
and access an element (row) with T row = list[y-1]
(zero-based) T
stands for string[]
, and we access an element with: string field = row[x - 1]
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];
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); } }
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("}"); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.