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(',');
解决方案static void getTwoArraysFromFile(string filein, ref double[] acc, ref double[] period)
{
string line;
List p1 = new List();
List p2 = new List();
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();
}