- Home /
Converting a .CSV string array to a float array
Hello all,
I'm trying to parse a .CSV file and store the contents as a float array.I've been trying to get this to work for a while now to no avail. My latest attempt is based on this post http://answers.unity3d.com/questions/601478/js-how-convert-a-string-to-int.html
Getting a string array from the file works, but I haven't found a way to get a float array going.
var TPRead : TextAsset;
var TPReadArr : String[];
var TPRList : List.<float>;
function Start () {
readFile();
}
function readFile() : List.<float> {
TPReadArr = TPRead.text.Split(","[0]);
var TPRList = new List.<float>();
for(var ctt : String in TPReadArr) {
TPRList.Add(float.Parse(ctt));
}
}
That code is getting me the following error
FormatException: Unknown char
System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Double.cs:209)
System.Single.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Single.cs:183)
CSV Reader.readFile () (at Assets/My Scripts/CSV Reader.js:29)
CSV Reader.Start () (at Assets/My Scripts/CSV Reader.js:20)
Which I don't understand. The string array shows up full of floats.
Here is the contents of the .CSV just in case.
38.81,40.64,59.3,49.16,41.75,38.56,39.79,52.3,47.38,91.69,53.68,54.28,51.89,49.05,52.98,49.96,55.86,53.28,49.91,50.84,48.25,59.12,52.76,49.76,60.89,48.52,49.41,62.37,57.24,59.25,58.91,72.31,80.13,43.46,73.96,60.4,70.23,82.6,95.4,73.28,61.44,53.98,51.81,48.39,46.43,41.41,41.43,37.73,50.38,47.82,52.55,45.82,49.83,43.86,48.72,46.58,52.21,49.4,128.96,31.24,38.81,37.49,36.67,57.01,63.34,55.77,0,0,0,0,36.25,32.92,39.99,39.63,40.89,37.33,34.79,36.37,36.49,31.88,31.39,31.13,35.95,36.29,32.86,29.22,33.68,34.02,37.46,34.66,36.78,36.97,38.06,39.99,40.23,30.2,0,0,43.11,42.47,39.39,40.47,45.27,41.98,81.81,71.66,76.03,259.72,99.12,0,388.14,33.48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
610.4,184.39,158.98,125.28,636.25,152.09,139.02,154.1,126.72,123.55,59.61,64.76,75.41,104.64,120.57,409.85,80.55,77.56,74.22,60.55,42.33,99.69,46.3,53.44,38.36,112.15,59.02,126.6,91.12,108.68,110.28,120.65,62.43,79.23,104.83,134.69,127.26,111.24,101,107.4,80.18,117.79,115.42,70.41,140.39,93.39,92.57,63.42,77.99,71.95,60.69,58.65,43.93,43.72,46.69,54.73,42.25,64.56,38.02,45.54,54.99,44.35,42.07,61.45,60.27,42.9,42.29,46.25,45.23,44.37,75.68,77.8,64.19,87.93,71.65,57.34,66.73,62.52,51.48,622.37,195.79,689.68,271.2,233.27,79.32,27.98,28.61,27.14,26.91,28.94,28.89,28.9,27.67,26.17,22.52,25.02,26.31,26.93,19.29,23.7,55.84,32.82,30.65,33.82,30.39,34.95,44.71,29.18,83.55,39.04,38.86,47.45,100.51,88.32,72.26,43.52,149.92,48.08,79.06,43.04,10.49,49.53,49.53,60.64,45.08,36.39,38.36,32.01,30.62,39.41,32.56,33.45,50.76,37.23,33.85,35.97,28.14,29.97,33.37,33.39,37.92,35.4,31.23,30.11,38.84,31.31,32.02,32.3,44.17,33.28,31.26,36.34,31.07,34.4,34.97,37,28.48,26.98,33.77,43,29.47,30.78,30.62,31.43,31.67,32.88,36.14,30.96,30.02,27.29,31.94,28.88,28.83,29.4,29.33,30.59,30.31,28.48,35.23,44.89,36.36,34.92
22.62,19.12,25.06,28,20.82,23.48,21.87,23.98,23.7,87.08,24.68,23.24,19.3,21.49,18.79,21.71,18.78,20.81,17.47,20.88,19.43,25.71,18.79,20.38,26.56,21.99,22.14,23.66,26.58,22.38,19.3,16.41,16.31,20.8,27.86,19.55,18.17,21.14,15.23,22.19,24.25,26.42,27.9,24.25,35.32,29.14,29.55,23.78,28.44,1433.99,616.33,37.35,167.4,35.33,37.78,35.15,26.75,44.43,92.7,259.08,240.44,28.65,87.47,23.46,32.77,22.44,31.27,22.01,70.7,185.85,226.73,201.21,95.28,61.66,27.19,20.57,37.38,22.21,433.69,318.06,131.97,97.69,91.05,76.87,151.69,173.82,141.59,308.27,317.5,497.64,777.7,238.97,155.5,748.81,746.84,458.93,194.25,98,82.14,80.1,47.89,37.78,45.94,32.18,35.93,41.29,46.52,62.11,53.49,53.56,115.12,126.76,51.67,47.06,39.35,38.89,31.7,25.33,46.11,38.72,44.28,41.52,42.45,43.37,44.3,45.22,46.15,47.07,41.33,54.66,87.01,71.6,65.12,60.67,92.17,75.57,70.76,93.42,96.09,120.72,69.11,73.21,76.48,87.95,77.31,70.15,53.23,74.26,107.61,139.21,60.39,75.85,75.26,75.85,77.42,125.19,66.27,70.14,90.7,106.09,74.2,82.06,69.61,66.94,75.2,131.41,185.93,72.29,59.95,63.55,61.15,50.55,56.61,55.73,48.96,58.26,50.51,56.26,51.51,64.47,68.05,52.05
82.1,62.43,80.29,82.53,60.66,52.39,55.21,62.28,71.3,67.53,52.08,56.31,53.41,53.13,185.02,78.78,77.51,75.41,75.13,526.72,107.5,67.09,85.26,127.22,1529.45,107.18,969.14,1234.53,463.12,704.8,1398.48,885.19,1905.76,591.68,621.09,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
If it's not all in one line, then you have return characters in there, so you'd have to deal with that...either convert them to commas before splitting on commas, or split on returns and then split each of those lines on commas.
You don't catch your exceptions. If you don't want to deal with exceptions, change your code to use float.TryParse().
If it's not all in one line, then you have return characters in there, so you'd have to deal with that...either convert them to commas before splitting on commas, or split on returns and then split each of those lines on commas.
That is a good point I didn't consider. Unfortunately, removing the returns didn't seem to change anything.
You don't catch your exceptions. If you don't want to deal with exceptions, change your code to use float.TryParse().
that results in BCE0023: No appropriate version of 'float.TryParse' for the argument list '(String)' was found.
Answer by Eric5h5 · Nov 15, 2014 at 07:24 PM
@Yog0: that's because TryParse doesn't take just a string. http://msdn.microsoft.com/en-us/library/system.int32.tryparse(v=vs.110).aspx But you still need to fix the issue with returns, regardless.