private static void ReadCsvCommodities2() { using (StreamReader sr = new StreamReader(@"TestData\commodities20200427.csv")) { Regex regex = new Regex(@"\d{4}"); string currentLine; Dictionary <string, Comm> values = new Dictionary <string, Comm>(); Dictionary <string, string> commodities = new Dictionary <string, string>(); commodities.Add("A", A); commodities.Add("C", C); commodities.Add("E", E); commodities.Add("I", I); commodities.Add("L", L); commodities.Add("M", M); commodities.Add("N", N); commodities.Add("P", P); commodities.Add("S", S); commodities.Add("X", X); // currentLine will be null when the StreamReader reaches the end of file while ((currentLine = sr.ReadLine()) != null) { string[] coulumns = currentLine.Split(new char[] { ';' }); Match match = regex.Match(coulumns[0]); if (match.Success) { string commValue = coulumns[1]; if (!commodities.ContainsKey(commValue)) { continue; } string id = match.Value; Comm comm; if (values.ContainsKey(id)) { comm = values[id]; } else { comm = new Comm(); values.Add(id, comm); } comm.Id = id; comm.Translation = commodities[commValue]; } } StringBuilder sb = new StringBuilder(); foreach (Comm item in values.Values) { //if (string.IsNullOrEmpty(item.Us)) //{ // continue; //} string commodityLine = string.Empty; //if (string.IsNullOrEmpty(item.De)) { // commodityLine = string.Format("{0};{1}", item.Us, item.Id); //} //else //{ // commodityLine = string.Format("{0}[de]{1};{2}", item.Us, item.De, item.Id); //} commodityLine = string.Format("{0};{1}", item.Translation, item.Id); sb.AppendLine(commodityLine); } File.WriteAllText(@"TestData\commoditiesFile.txt", sb.ToString()); } }
private static void CheckCsvCommodities() { Regex regex = new Regex(@"\d{4}"); Dictionary <string, Comm> values = new Dictionary <string, Comm>(); Dictionary <string, string> commodities = new Dictionary <string, string>(); commodities.Add("BA", BA); commodities.Add("BC", BC); commodities.Add("BI", BI); commodities.Add("BE", BE); commodities.Add("BM", BM); commodities.Add("BN", BN); commodities.Add("BP", BP); commodities.Add("BX", BX); StringBuilder sb = new StringBuilder(); List <string> matGroup = new List <string>(); using (StreamReader sr = new StreamReader(@"C:\kpl\commodities.csv")) { string currentLine; // currentLine will be null when the StreamReader reaches the end of file while ((currentLine = sr.ReadLine()) != null) { string[] coulumns = currentLine.Split(new char[] { ';' }); if (coulumns.Length > 1) { string commValue = coulumns[0]; string id = coulumns[1]; if (!commodities.ContainsKey(commValue)) { //sb.AppendLine(string.Format("unknown KONZ_COMMODITY:{0}", commValue)); continue; } Comm comm; if (values.ContainsKey(id)) { comm = values[id]; } else { comm = new Comm(); values.Add(id, comm); } comm.Id = id; comm.Translation = commodities[commValue]; } } } using (StreamReader sr = new StreamReader(@"C:\kpl\sourcecsv28Group.csv")) { string currentLine; // currentLine will be null when the StreamReader reaches the end of file while ((currentLine = sr.ReadLine()) != null) { if (!matGroup.Contains(currentLine)) { matGroup.Add(currentLine); } } } foreach (string item in matGroup) { if (!values.ContainsKey(item)) { string commodityLine = string.Format("group:{0} not found in commodity file", item); sb.AppendLine(commodityLine); } } File.WriteAllText(@"C:\kpl\missingCommodities.txt", sb.ToString()); }
private static void ReadCsvCommodities() { using (StreamReader sr = new StreamReader(@"C:\kpl\commodities.csv")) { Regex regex = new Regex(@"\d{4}"); string currentLine; Dictionary <string, Comm> values = new Dictionary <string, Comm>(); Dictionary <string, string> commodities = new Dictionary <string, string>(); commodities.Add("BA", BA); commodities.Add("BC", BC); commodities.Add("BI", BI); commodities.Add("BE", BE); commodities.Add("BM", BM); commodities.Add("BN", BN); commodities.Add("BP", BP); commodities.Add("BX", BX); // currentLine will be null when the StreamReader reaches the end of file while ((currentLine = sr.ReadLine()) != null) { string[] coulumns = currentLine.Split(new char[] { ';' }); Match match = regex.Match(coulumns[1]); if (match.Success) { string commValue = coulumns[0]; if (!commodities.ContainsKey(commValue)) { continue; } string id = match.Value; Comm comm; if (values.ContainsKey(id)) { comm = values[id]; } else { comm = new Comm(); values.Add(id, comm); } comm.Id = id; comm.Translation = commodities[commValue]; //if (coulumns[2].Equals("US", StringComparison.InvariantCultureIgnoreCase)) //{ // comm.Us = coulumns[3]; //} //else if (coulumns[2].Equals("D", StringComparison.InvariantCultureIgnoreCase)) //{ // comm.De = coulumns[3]; //} } } StringBuilder sb = new StringBuilder(); foreach (Comm item in values.Values) { //if (string.IsNullOrEmpty(item.Us)) //{ // continue; //} string commodityLine = string.Empty; //if (string.IsNullOrEmpty(item.De)) { // commodityLine = string.Format("{0};{1}", item.Us, item.Id); //} //else //{ // commodityLine = string.Format("{0}[de]{1};{2}", item.Us, item.De, item.Id); //} commodityLine = string.Format("{0};{1}", item.Translation, item.Id); sb.AppendLine(commodityLine); } File.WriteAllText(@"C:\kpl\commoditiesFile.txt", sb.ToString()); } }