Reputation: 193
I have a situation that need to make two arrays from each column of csv file. As shown below, the csv file contain two columns each column has a header titled 'period' and 'acceleration'.
Period,Acceleration
0.01,0.6
0.05,0.82
0.1,1.26
0.15,1.403
0.2,1.383
I tried to use following code and then split this into two arrays. However, it did not break the numbers by comma.
string[] allLines = File.ReadAllText(@"C:\ArsScale\Tars.csv").Split(',');
Upvotes: 0
Views: 2944
Reputation: 3588
String.Split is not a robust way of parsing CSV fields, as they may contain commas within the fields etc
In any case you need to use File.ReadAllLines
, not File.ReadAllText
File.ReadAllLines gives you a array of strings - 1 for each line.
You could then iterate through the array and copy to 2 other arrays for each item.
Example:
var lines = File.ReadAllLines (@"C:\ArsScale\Tars.csv");
var periods = lines.Skip(1).Select(x => x.Split(",")[0]).ToArray();
var accelerations = lines.Skip(1).Select(x => x.Split(",")[1]).ToArray();
However I'd advise a more robust method.
Have a look at this article: http://www.codeproject.com/Articles/415732/Reading-and-Writing-CSV-Files-in-Csharp
Upvotes: 0
Reputation: 1790
Use a StreamReader to read your csv line by line. Then split each line and add each value to a list. Finally create two arrays from your lists.
List<Double> periodList = new List<Double>();
List<Double> accelerationList = new List<Double>();
StreamReader file = new System.IO.StreamReader(@"C:\ArsScale\Tars.csv");
string line = file.ReadLine();
while ((line = file.ReadLine()) != null)
{
string[] data = line.Split(',');
periodList.Add(Convert.ToDouble(data[0]);
accelerationList.Add(Convert.ToDouble(data[1]);
}
Double[] periodArray = periodList.ToArray();
Double[] accelerationArray = accelerationList.ToArray();
Upvotes: 1
Reputation: 1582
static void getTwoArraysFromFile(string filein, ref double[] acc, ref double[] period)
{
string line;
List<double> p1 = new List<double>();
List<double> p2 = new List<double>();
System.IO.StreamReader file = new System.IO.StreamReader(filein);
while ((line = file.ReadLine()) != null)
try {
String[] parms = line.Trim().Split(',');
p1.Add(double.Parse(parms[1], CultureInfo.InvariantCulture));
p2.Add(double.Parse(parms[0], CultureInfo.InvariantCulture));
}
catch { }
acc = p1.ToArray();
period = p2.ToArray();
}
Upvotes: 4
Reputation: 4923
IEnumerable<string[]> allLines = File.ReadAllLines(@"C:\ArsScale\Tars.csv").Select(x => x.Split(','));
This will read all the lines from the text file, then split each line. The datatype will be IEnumerable
of string[]
. To change this to string[][]
, simply call .ToArray()
after the Select
statement.
This method is quick and simple, however it does absolutely no validation on the input. For example, the CSV spec allows for commas to be present inside of values as long as they are escaped. If you need to have validation of any kind, you need to look into a CSV parser, of which there are many.
If you need no validation, you're positive about the input, and don't care about good error handling, you can use the following:
var allLines = File.ReadAllLines(@"C:\ArsScale\Tars.csv").Select(x => x.Split(',').Select(y => double.Parse(y).ToArray())).ToArray();
This will give you double[][]
as your output.
Upvotes: 2