Reputation: 679
I have a .csv file that looks like this:
Index,Time,value
1,0,20
2,1,30
What I want to do is to open the .csv file in C# and then read it by getting row = 0, column = 0
which would give me value 1
in the case above. Much like this:
public double GetVal(int row, int column)
{
...
return val;
}
I have looked around for a solution, for example this one: Remove values from rows under specific columns in csv file
But I need to be able to specify both the column and row in the function to get the specific value.
Upvotes: 1
Views: 6974
Reputation: 186668
In case CSV file is simple one (no quotations), you can try Linq:
using System.IO;
using System.Linq;
...
public double GetVal(int row, int column) {
return File
.ReadLines(@"c:\MyFile.csv") //TODO: put the right name here
.Where(line => !string.IsNullOrWhiteSpace(line)) // to be on the safe side
.Skip(1) // Skip line with Titles
.Skip(row)
.Select(line => double.Parse(line.Split(',')[column]))
.First();
}
Note, that this code re-reads the file; you may want to read the file once:
string[][] m_Data = File
.ReadLines(@"c:\MyFile.csv")
.Where(line => !string.IsNullOrWhiteSpace(line))
.Skip(1)
.Select(line => line.Split(','))
.ToArray();
...
public double GetVal(int row, int column) => double.Parse(m_Data[row][col]);
Upvotes: 2
Reputation: 16277
Quick answer, without considering the performance issue (e.g. the file read should happen once, and other issue like index checking to avoid overflows.
public double GetVal(int row, int column)
{
double output;
using (var reader = new StreamReader("filename.csv"))
{
int m = 1;
while (!reader.EndOfStream)
{
if(m==row)
{
var splits = rd.ReadLine().Split(',');
//You need check the index to avoid overflow
output = double.Parse(splits[column]);
return output;
}
m++;
}
}
return output;
}
Upvotes: 0
Reputation: 2000
Although you can write simple code by yourself, I would suggest you using dedicated CSV library for this like https://www.nuget.org/packages/LumenWorksCsvReader/ There are tons of edge cases like values escaping with double quotes, multiline values in CSV file format.
But if you totally control your files and they are small, you can read all lines at once and parse them. Something like this to get all lines from file and then split every line by ',' character
var lines = File.ReadAllLines('your file');
Upvotes: 0