/// <summary> /// finds CSV values. filters out non numeric values /// </summary> /// <param name="str"></param> /// <returns></returns> public static List <float> extractValues(string str) { List <string> vals = ParsingUtils.separateCSV(str); List <float> valsF = new List <float>(); foreach (var v in vals) { string cs = ParsingUtils.filterOutNonNumericValues(v); if (cs.Length > 0) { valsF.Add(float.Parse(cs)); } } return(valsF); }
public static List <Dictionary <string, string> > readTable(string[] tableLines) { tableLines = ParsingUtils.clearComments(tableLines); List <string> headerLine = ParsingUtils.separateCSV(tableLines[0]); List <Dictionary <string, string> > res = new List <Dictionary <string, string> >(); for (int l = 1; l < tableLines.Count(); ++l) { Dictionary <string, string> lineVals = new Dictionary <string, string>(); List <string> vals = ParsingUtils.separateCSV(tableLines[l]); for (int vi = 0; vi < vals.Count; ++vi) { lineVals[headerLine[vi].Trim()] = vals[vi].Trim(); } res.Add(lineVals); } return(res); }
/// <summary> /// NOTE: white space in values will be disregarded (and removed when calling readTable()) /// </summary> /// <param name="vals"></param> /// <param name="fileout"></param> public static List <string> ToTable(List <Dictionary <string, string> > vals) { List <string> tableLines = new List <string>(); HashSet <string> commonHeadersHash = new HashSet <string>(); // headers that repeat in all lines HashSet <string> uncommonHeadersHash = new HashSet <string>(); // add all headers foreach (var d in vals) { if (d == null) { continue; } foreach (string ks in d.Keys) { if (ks == null || ks == "") { continue; } commonHeadersHash.Add(ks); } } // remove all non common headers: foreach (var d in vals) { HashSet <string> dupeTable = new HashSet <string>(commonHeadersHash); foreach (string ks in dupeTable) { if (!d.ContainsKey(ks)) { commonHeadersHash.Remove(ks); uncommonHeadersHash.Add(ks); } } } // move to lists to make sure the order doesn't change: List <string> commonHeaders = new List <string>(commonHeadersHash); List <string> uncommonHeaders = new List <string>(uncommonHeadersHash); // we want to keep similar distance between columns and values int entryLength = 0; foreach (var d in vals) { foreach (var v in commonHeaders) { entryLength = Math.Max(entryLength, v.Length); entryLength = Math.Max(entryLength, d[v].Length); } foreach (var v in uncommonHeaders) { entryLength = Math.Max(entryLength, v.Length); if (d.ContainsKey(v)) { entryLength = Math.Max(entryLength, d[v].Length); } } } entryLength++; // insure there is always at least 1 space between entries // add column names: List <string> columnNames = new List <string>(commonHeaders); columnNames.AddRange(uncommonHeaders); tableLines.Add(ParsingUtils.makeCSV(columnNames, entryLength, true)); foreach (var d in vals) { List <string> rowVals = new List <string>(); // values in d that are under common headers, and then the unique foreach (string s in commonHeaders) { rowVals.Add(d[s]); } foreach (string s in uncommonHeaders) { if (d.ContainsKey(s)) { rowVals.Add(d[s]); } else { rowVals.Add("-1"); } } tableLines.Add(ParsingUtils.makeCSV(rowVals, entryLength, true)); } return(tableLines); }