/// <summary> /// Get an array of double's from a string of comma separated text. /// </summary> /// <param name="format">The way to format this list.</param> /// <param name="str">The string that contains a list of numbers.</param> /// <returns>An array of doubles parsed from the string.</returns> public static double[] FromList(CSVFormat format, String str) { if( str.Trim().Length==0) { return new double[0]; } // first count the numbers String[] tok = str.Split(format.Separator); int count = tok.Length; // now allocate an object to hold that many numbers var result = new double[count]; // and finally parse the numbers for (int index = 0; index < tok.Length; index++) { try { String num = tok[index]; double value = format.Parse(num); result[index] = value; } catch (Exception e) { throw new PersistError(e); } } return result; }
/// <summary> /// Get an array of double's from a string of comma separated text. /// </summary> /// <param name="format">The way to format this list.</param> /// <param name="str">The string that contains a list of numbers.</param> /// <returns>An array of doubles parsed from the string.</returns> public static double[] FromList(CSVFormat format, String str) { // first count the numbers String[] tok = str.Split(format.Separator); int count = tok.Length; // now allocate an object to hold that many numbers double[] result = new double[count]; // and finally parse the numbers for (int index = 0; index < tok.Length; index++) { try { String num = tok[index]; double value = format.Parse(num); result[index] = value; } catch (Exception e) { throw new PersistError(e); } } return(result); }
public static double[] FromList(CSVFormat format, string str) { if (str.Trim().Length != 0) { string[] strArray = str.Split(new char[] { format.Separator }); int length = strArray.Length; double[] numArray = new double[length]; for (int i = 0; i < strArray.Length; i++) { try { string str2 = strArray[i]; numArray[i] = format.Parse(str2); } catch (Exception exception) { throw new PersistError(exception); } } return numArray; } return new double[0]; }
/// <summary> /// Get the specified column as a double. /// </summary> /// <param name="column">The column to read.</param> /// <returns>The specified column as a double.</returns> public double GetDouble(String column) { String str = Get(column); return(_format.Parse(str)); }
/// <summary> /// Analyze the input file. /// </summary> /// <param name="input">The input file.</param> /// <param name="headers">True, if there are headers.</param> /// <param name="format">The format of the CSV data.</param> public virtual void Analyze(FileInfo input, bool headers, CSVFormat format) { ResetStatus(); InputFilename = input; ExpectInputHeaders = headers; Format = format; _columnMapping.Clear(); _columns.Clear(); // first count the rows TextReader reader = null; try { int recordCount = 0; reader = new StreamReader(InputFilename.OpenRead()); while (reader.ReadLine() != null) { UpdateStatus(true); recordCount++; } if (headers) { recordCount--; } RecordCount = recordCount; } catch (IOException ex) { throw new QuantError(ex); } finally { ReportDone(true); if (reader != null) { try { reader.Close(); } catch (IOException e) { throw new QuantError(e); } } InputFilename = input; ExpectInputHeaders = headers; Format = format; } // now analyze columns ReadCSV csv = null; try { csv = new ReadCSV(input.ToString(), headers, format); if (!csv.Next()) { throw new QuantError("File is empty"); } for (int i = 0; i < csv.ColumnCount; i++) { String name; if (headers) { name = AttemptResolveName(csv.ColumnNames[i]); } else { name = "Column-" + (i + 1); } // determine if it should be an input/output field String str = csv.Get(i); bool io = false; try { Format.Parse(str); io = true; } catch (FormatException ex) { EncogLogging.Log(ex); } AddColumn(new FileData(name, i, io, io)); } } finally { if (csv != null) csv.Close(); Analyzed = true; } }