public static void Compare(String handsOnFileName, String exportedFileName, String missingFileName, String newFileName) { ExtendedCSV extended = new ExtendedCSV( new FileStream(handsOnFileName, FileMode.Open), new List <string>() { "WASP" } ); CSV other = new CSV(new FileStream(exportedFileName, FileMode.Open)); extended.GetMissingRowsFrom(other).Save(missingFileName); extended.GetExtraRowsFrom(other).Save(newFileName); }
public static void Combine() { List <String> files = Directory.EnumerateFiles("/Users/jcox/Documents/toCombine/").Where( fn => fn.EndsWith(".csv", StringComparison.InvariantCultureIgnoreCase)).ToList(); CSV output = new CSV(); foreach (string fileName in files) { CSV toAdd = new CSV(new FileStream(fileName, FileMode.Open)); output.Add(toAdd); } output.Save("/Users/jcox/Documents/combined.csv"); }
/// <summary> /// Query this table and find all rows that match a set of regular expressions. /// Use this to find all rows where a data fits a particular set of regular expressions. /// for example, find all rows in a contact list where the Phone Number has a 540 area code /// and the street address is in a particular town. /// </summary> /// <param name="primaryKey"></param> /// <returns></returns> public CSV this[Dictionary <string, Regex> primaryKey] { get { CSV output = new CSV(); foreach (Dictionary <String, String> row in this) { foreach (string key in primaryKey.Keys) { if (!row.ContainsKey(key) || !primaryKey[key].IsMatch(row[key])) { continue; } } output.Add(row); } return(output); } }
public CSV PullRowsMatchingPrimaryKeysWith(CSV other) { CSV output = new CSV(); foreach (Dictionary <String, String> row in other.Data) { try { output.Add(this.Find(row)); } catch (KeyNotFoundException) { continue; } catch (InvalidDataException) { continue; } } return(output); }
/// <summary> /// Gets the extra rows from this csv which are not present in the other csv. /// </summary> /// <returns>The extra rows from.</returns> /// <param name="other">Other.</param> public CSV GetExtraRowsFrom(CSV other) { CSV output = new CSV(); foreach (Dictionary <String, String> row in this.Data) { bool found = false; foreach (Dictionary <string, string> checkRow in other.Data) { if (CompareUniqueFields(checkRow, row)) { found = true; break; } } if (!found) { output.Add(row); } } return(output); }
/// <summary> /// Initializes a new instance of the <see cref="T:CSVDataManipulation.ExtendedCSV"/> class. /// </summary> /// <param name="toExtend">To extend.</param> /// <param name="uidfields">Uidfields.</param> public ExtendedCSV(CSV toExtend, List <string> uidfields) : base(toExtend.Data) { this.UniqueFields = uidfields; }
public static void Flatten() { List <String> files = Directory.EnumerateFiles(String.Format("/Users/{0}/Documents/toFlatten/", Environment.UserName)).ToList(); if (!Directory.Exists(String.Format("/Users/{0}/Documents/flattened", Environment.UserName))) { Directory.CreateDirectory(String.Format("/Users/{0}/Documents/flattened", Environment.UserName)); } List <String> uniqueFields = new List <string>(); if (File.Exists(String.Format("/Users/{0}/Documents/toFlatten/.flatten.conf", Environment.UserName))) { StreamReader reader = new StreamReader( new FileStream( String.Format("/Users/{0}/Documents/toFlatten/.flatten.conf", Environment.UserName), FileMode.Open)); String fields = reader.ReadLine(); uniqueFields.AddRange(fields.Split('\t')); } else { // ask the user. Console.WriteLine("What are the unique fields? [Enter one at a time. '*' to End."); String field = ""; while (true) { Console.Write(">> "); field = Console.ReadLine(); if (field.Equals("*")) { break; } else { uniqueFields.Add(field); } } } foreach (string fileName in files) { if (!fileName.EndsWith(".csv", StringComparison.CurrentCultureIgnoreCase)) { continue; } Console.WriteLine(fileName); FileStream fileStream = new FileStream(fileName, FileMode.Open); ExtendedCSV extendedCSV = new ExtendedCSV(fileStream, uniqueFields) { ConflictRule = new YesBeatsNoConflictRule() { NextRule = new UserResolveConflictRule() } }; CSV output = extendedCSV.FlattenRows(); string outfilename = fileName.Replace("toFlatten", "flattened"); output.Save(outfilename); Console.WriteLine("Flattened: {0}", outfilename); } Console.WriteLine("Done!"); Console.ReadKey(); }