public IEnumerable<string> Run() { var onlyLogErrors = bool.Parse(ConfigurationManager.AppSettings["OnlyLogErrors"]); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(_command.Datafile); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.SkipStartingDataRows = 0; parser.MaxBufferSize = 4096; parser.MaxRows = 1000000; parser.TextQualifier = '\"'; _connection.Open(); var count = 0; while (parser.Read()) { SetParameters(parser); count++; Exception exception = null; var affected = 0; try { affected = _adaptor.Execute(); } catch (Exception ex) { exception = ex; } if (exception != null) yield return exception.ToString(); else if (affected == 0) yield return count.ToString() + " : " + FailMessage(parser); else if (!onlyLogErrors) yield return count.ToString() + " : " + SuccessMessage(parser); else yield return ""; } _connection.Close(); } }
static Data ReadFieldData(string fname) { var d = new Data(); var rows = new List<double[]>(); var labels = new List<string>(); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(fname); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = false; parser.TextQualifier = '\"'; while(parser.Read()) { rows.Add(StringKernalFunction(parser[0].Trim())); labels.Add(parser[1]); } } d.Features = rows.ToArray(); d.Labels = labels.ToArray(); return d; }
private void btn2_Click(object sender, EventArgs e) { //ArgumentParser argParser = new ArgumentParser(args); String dataSource = ofd1.FileName;//argParser["o"]; if (dataSource != null) { try { GenericParser parser = new GenericParser(); parser.SetDataSource(dataSource); parser.FirstRowHasHeader = true; parser.Read(); int columnCount = parser.ColumnCount; //Work out the names of the Accounts we need to sum List<string> ItemNames = new List<string>(); for (int i = 0; i < columnCount; i++) { ItemNames.Add (parser.GetColumnName(i)); } // //Work out the numbers we need to sum // List<Decimal> DecimalsToSum = new List<decimal>(); // for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++) // { // DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter])); // } // // List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>(); // List<Tuple<Decimal,Decimal,List<String>>> AbsSumsAndOps = new List<Tuple<Decimal,Decimal,List<String>>>(); // List<String> OpsSoFar = new List<string>(); // // callsCount = 0; // BuildPathsAndSumsTogether (SumsAndOps, AbsSumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount); // SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); // AbsSumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); //Work out the numbers we need to sum List<Decimal> DecimalsToSum = new List<decimal>(); for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++) { DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter])); } //List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>(); List<String> OpsSoFar = new List<string>(); callsCount = 0; maxAbsDiff = -9999; BuildPathsAndSumsTogetherMinimal(DecimalsToSum, OpsSoFar, 0, 0, columnCount); String stuff = "yarr"; Decimal outcome = maxAbsDiff; //BuildPathsAndSumsTogetherNoAbs (SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount);(SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount); //SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); // int countSuccess = 0; // textBox1.Text = ""; // foreach(Tuple<Decimal,List<string>> sumWithOps in SumsAndOps) { // if(sumWithOps.Item1 == 0) { // countSuccess++; // // for(int account = 0; account < ItemNames.Count; account++) { // textBox1.Text += (ItemNames[account].PadLeft(30) + ":" // + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4) // + "(" + sumWithOps.Item2[account] + ")" // + Environment.NewLine); // Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item2[account]); // } // textBox1.Text += ("Balances To".PadLeft(30) + ":" // + sumWithOps.Item1.ToString().PadLeft(15) // + Environment.NewLine + Environment.NewLine); // Console.WriteLine("Balances To" +":"+ sumWithOps.Item1); // Console.WriteLine(); // } // } // // if(countSuccess == 0) { // int n = 20; // //// textBox1.Text = "This combination of numbers has no possible way to balance. The " + n + " solutions closest to 0 follow." + Environment.NewLine; //// //// for(int closeResultPos = 0 ; closeResultPos < n || closeResultPos >= AbsSumsAndOps.Count; closeResultPos++) { //// Tuple<Decimal,Decimal,List<string>> sumWithOps = AbsSumsAndOps[closeResultPos]; //// for(int account = 0; account < ItemNames.Count; account++) { //// textBox1.Text += (ItemNames[account].PadLeft(30) + ":" //// + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4) //// + "(" + sumWithOps.Item3[account] + ")" //// + Environment.NewLine); //// Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item3[account]); //// } //// //// textBox1.Text += ("Balances To".PadLeft(30) + ":" //// + sumWithOps.Item2.ToString().PadLeft(15) //// + Environment.NewLine + Environment.NewLine); //// Console.WriteLine("Balances To" +":"+ sumWithOps.Item2); //// Console.WriteLine(); //// } // } } catch (Exception ex) { textBox1.Text = ex.Message; } } }
public static DataTable CsvToDataTable(string path, Dictionary <string, Type> expectedColumnsAndTypes, char seperator, string dateTimeFormat, CultureInfo cultureInfo = null) { if (cultureInfo == null) { cultureInfo = CultureInfo.InvariantCulture; } DataTable dt = new DataTable(); foreach (var pair in expectedColumnsAndTypes) { dt.Columns.Add(pair.Key, pair.Value); } using (GenericParser parser = new GenericParser()) { parser.SetDataSource(path); parser.ColumnDelimiter = seperator; parser.FirstRowHasHeader = true; //parser.SkipStartingDataRows = 10; parser.MaxBufferSize = 4096; parser.MaxRows = int.MaxValue; parser.TextQualifier = '\"'; parser.SkipEmptyRows = true; while (parser.Read()) { DataRow row = dt.NewRow(); foreach (var pair in expectedColumnsAndTypes) { //OrderUuid,Exchange,Type,Quantity,Limit,CommissionPaid,Price,Opened,Closed string value = parser[pair.Key]; if (string.IsNullOrWhiteSpace(value)) { continue; } if (pair.Value == typeof(decimal)) { row[pair.Key] = string.IsNullOrEmpty(value) ? 0 : decimal.Parse(value, cultureInfo); } else if (pair.Value == typeof(DateTime)) { row[pair.Key] = DateTime.ParseExact(value, dateTimeFormat, cultureInfo); } else { row[pair.Key] = value; } } dt.Rows.Add(row); } } return(dt); }
public IEnumerable <string> Run() { var onlyLogErrors = bool.Parse(ConfigurationManager.AppSettings["OnlyLogErrors"]); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(_command.Datafile); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.SkipStartingDataRows = 0; parser.MaxBufferSize = 4096; parser.MaxRows = 1000000; parser.TextQualifier = '\"'; _connection.Open(); var count = 0; while (parser.Read()) { SetParameters(parser); count++; Exception exception = null; var affected = 0; try { affected = _adaptor.Execute(); } catch (Exception ex) { exception = ex; } if (exception != null) { yield return(exception.ToString()); } else if (affected == 0) { yield return(count.ToString() + " : " + FailMessage(parser)); } else if (!onlyLogErrors) { yield return(count.ToString() + " : " + SuccessMessage(parser)); } else { yield return(""); } } _connection.Close(); } }
static public DataTable loadCSVToDataTable(string tableName, string path) { DataTable dt = new System.Data.DataTable(tableName); using (GenericParser parser = new GenericParser()) { try { parser.SetDataSource(path); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.MaxBufferSize = 1024; bool isfirst = true; int count = 0; while (parser.Read()) { count++; int cs = parser.ColumnCount; if (isfirst) { for (int i = 0; i < cs; i++) { dt.Columns.Add(parser.GetColumnName(i), typeof(string)); } isfirst = false; } DataRow dr = dt.NewRow(); for (int i = 0; i < cs; i++) { string val = parser[i]; dr[i] = val; } dt.Rows.Add(dr); } } catch (Exception ex) { logger.Error(ex, "Exception:"); } } return(dt); }
private DataTable ReadDataFromFile() { var tbl = new DataTable(); using (var parser = new GenericParser()) { parser.TextFieldType = FieldType.Delimited; parser.ColumnDelimiter = config.Delimiter[0]; parser.FirstRowHasHeader = true; parser.FirstRowSetsExpectedColumnCount = true; parser.TrimResults = false; //parser.SkipStartingDataRows = 10; parser.MaxBufferSize = 1048576; //parser.MaxRows = 500; parser.TextQualifier = '\"'; parser.EscapeCharacter = null; parser.SetDataSource(config.InputFile, Encoding.UTF8); bool doSetup = true; while (parser.Read()) { if (doSetup) { for (int i = 0; i < parser.ColumnCount; i++) { string colName = parser.GetColumnName(i); tbl.Columns.Add(colName, typeof(string)); } doSetup = false; } var row = tbl.NewRow(); for (int i = 0; i < parser.ColumnCount; i++) { string colName = parser.GetColumnName(i); row[colName] = parser[colName]; } tbl.Rows.Add(row); } } return(tbl); }
public static void GetAssurContrCompSubsidFromCSV(ref List <string> data, string filePath, int importId = 0) { GenericParser parser; try { string assur, contr, comp, subsid; string accs; using (parser = new GenericParser()) { parser.SetDataSource(filePath); parser.ColumnDelimiter = C.cVALSEP.ToCharArray()[0]; parser.FirstRowHasHeader = true; //parser.SkipStartingDataRows = 10; //parser.MaxBufferSize = 4096; //parser.MaxRows = 500; //parser.TextQualifier = '\"'; while (parser.Read()) { assur = parser[C.eDBTempOtherFieldsColumns.AssureurName.ToString()]; contr = parser[C.eDBTempOtherFieldsColumns.ContractId.ToString()]; comp = parser[C.eDBTempOtherFieldsColumns.Company.ToString()]; subsid = parser[C.eDBTempOtherFieldsColumns.Subsid.ToString()]; accs = importId.ToString() + C.cVALSEP + assur + C.cVALSEP + contr + C.cVALSEP + comp + C.cVALSEP + subsid + C.cVALSEP + C.cVALSEP; if (!string.IsNullOrWhiteSpace(accs) && !data.Contains(accs)) { data.Add(accs); } } } } catch (Exception ex) { log.Error(ex.Message); throw ex; } }
public List <string[]> ReadCSVFile(string path) { List <string[]> components = new List <string[]>(); try { ///Set variables to store values in string ObjectName; string ObjectType; using (GenericParser parser = new GenericParser()) { parser.SetDataSource(path); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.MaxBufferSize = 4096; parser.MaxRows = 50000; parser.TextQualifier = '\"'; parser.SkipEmptyRows = true; while (parser.Read()) { ObjectName = parser["Object Name"]; ObjectType = parser["Object Type"]; string[] component = new string[] { ObjectName, ObjectType }; components.Add(component); } return(components); } } catch { AppendTextBox("Error reading CSV file"); SetStatus("ERROR", Brushes.Red); return(components); } }
public List <LicenseDetails> loadCSV(string Filepath, string ApplicationID) { try { List <LicenseDetails> lstLicenseData = new List <LicenseDetails>(); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(Filepath); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.TextQualifier = '\"'; while (parser.Read()) { if (parser["ApplicationID"] == ApplicationID) { LicenseDetails licenseDetails = new LicenseDetails(); licenseDetails.ComputerID = parser["ComputerID"]; licenseDetails.UserID = parser["UserID"]; licenseDetails.ApplicationID = parser["ApplicationID"]; licenseDetails.ComputerType = parser["ComputerType"]; licenseDetails.Comment = parser["Comment"]; lstLicenseData.Add(licenseDetails); } } } return(lstLicenseData); } catch (Exception) { throw new Exception("File does not exist or bad data"); } }
private void BtnLoad_Click(object sender, EventArgs e) { importData = new Model.ImportDataDataTable(); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(txtImportFile.Text); parser.ColumnDelimiter = ';'; parser.FirstRowHasHeader = true; parser.SkipStartingDataRows = 0; parser.MaxBufferSize = 4096; parser.MaxRows = 500; parser.TextQualifier = '\"'; while (parser.Read()) { Model.ImportDataRow row = importData.NewImportDataRow(); row.Value = int.Parse(parser["Value"]); if (!string.IsNullOrEmpty(parser["Label1031"])) { row.Label1031 = parser["Label1031"]; if (!string.IsNullOrEmpty(row.Label1031)) { row.Label1031 = row.Label1031.Replace("\"", string.Empty).Trim(); } } if (!string.IsNullOrEmpty(parser["Label1033"])) { row.Label1033 = parser["Label1033"]; if (!string.IsNullOrEmpty(row.Label1033)) { row.Label1033 = row.Label1033.Replace("\"", string.Empty).Trim(); } } if (!string.IsNullOrEmpty(parser["Label1036"])) { row.Label1036 = parser["Label1036"]; if (!string.IsNullOrEmpty(row.Label1036)) { row.Label1036 = row.Label1036.Replace("\"", string.Empty).Trim(); } } if (!string.IsNullOrEmpty(parser["Label1040"])) { row.Label1040 = parser["Label1040"]; if (!string.IsNullOrEmpty(row.Label1040)) { row.Label1040 = row.Label1040.Replace("\"", string.Empty).Trim(); } } if (!string.IsNullOrEmpty(parser["Label3082"])) { row.Label3082 = parser["Label3082"]; if (!string.IsNullOrEmpty(row.Label3082)) { row.Label3082 = row.Label3082.Replace("\"", string.Empty).Trim(); } } importData.AddImportDataRow(row); } //while } //using grdValues.DataSource = importData; }//BtnLoad_Click
public void Load(string ChangesFilePath) { using (GenericParser parser = new GenericParser()) { parser.SetDataSource(ChangesFilePath); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.TextQualifier = '\"'; //parser.SkipStartingDataRows = 10; //parser.MaxBufferSize = 4096; //parser.MaxRows = 500; int ColumnCount = 0; string[] ReservedColumnNames = new string[] { "table", "elementType", "elementName" }; while (parser.Read()) { // get the column count on the first pass if (ColumnCount == 0) { ColumnCount = parser.ColumnCount; } string Table = parser["table"]; string ElementType = parser["elementType"]; string ElementName = parser["elementName"]; // determine the type of change JsonChange change = null; if (ElementType.Equals("column", StringComparison.CurrentCultureIgnoreCase)) { change = new JsonChangeColumn() { Table = Table, Column = ElementName }; } if (ElementType.Equals("measure", StringComparison.CurrentCultureIgnoreCase)) { change = new JsonChangeMeasure() { Table = Table, Measure = ElementName }; } ; // load the additional columns for (int ColumnIndex = 0; ColumnIndex < ColumnCount; ColumnIndex++) { string ColumnName = parser.GetColumnName(ColumnIndex); // ignore reserved columns names if (ReservedColumnNames.Contains <string>(ColumnName, StringComparer.CurrentCultureIgnoreCase)) { continue; } // add the attribute string Value = parser[ColumnIndex]; if (!String.IsNullOrEmpty(Value)) { change.Attributes.Add(new JsonAttribute(ColumnName, parser[ColumnIndex])); } } this.Add(change); } } }
private void button2_Click(object sender, EventArgs e) { if (txtbhavcopy.Text != string.Empty) { string parseDate(string indate) { DateTime date1 = Convert.ToDateTime(indate); var month = date1.ToString("MMMM"); var year = date1.ToString("yyyy"); var date = date1.ToString("dd"); string desiredMonth = month; string[] MonthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames; int monthIndex = Array.IndexOf(MonthNames, desiredMonth) + 1; int digits = Convert.ToString(monthIndex).Length; if (digits > 1) { return(year + monthIndex + date); } else { return(year + "0" + monthIndex + date); } } using (GenericParser parser = new GenericParser()) { parser.SetDataSource(txtbhavcopy.Text); parser.FirstRowHasHeader = true; parser.SkipStartingDataRows = 0; parser.MaxBufferSize = 4096; parser.TextQualifier = '\"'; while (parser.Read()) { var x = parseDate(parser["TIMESTAMP"]); ListItem item = new ListItem() { SYMBOL = parser["SYMBOL"], TIMESTAMP = parseDate(parser["TIMESTAMP"]), SERIES = parser["SERIES"], OPEN = parser["OPEN"], HIGH = parser["HIGH"], LOW = parser["LOW"], CLOSE = parser["CLOSE"], TOTTRDQTY = parser["TOTTRDQTY"], extrafield = "0" }; currentdatebhav = item.TIMESTAMP; items.Add(item); } converttotxt(); } } else { MessageBox.Show("Please select BhavCopy File"); } }
private void button3_Click(object sender, EventArgs e) { if (textBox1.Text != string.Empty) { string parseDate(string indate) { DateTime date1 = Convert.ToDateTime(indate); var month = date1.ToString("MMMM"); var year = date1.ToString("yyyy"); var date = date1.ToString("dd"); string desiredMonth = month; string[] MonthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames; int monthIndex = Array.IndexOf(MonthNames, desiredMonth) + 1; int digits = Convert.ToString(monthIndex).Length; if (digits > 1) { return(year + monthIndex + date); } else { return(year + "0" + monthIndex + date); } } using (GenericParser parser = new GenericParser()) { parser.SetDataSource(textBox1.Text); parser.FirstRowHasHeader = true; parser.SkipStartingDataRows = 0; parser.MaxBufferSize = 4096; parser.TextQualifier = '\"'; while (parser.Read()) { IndexItem item = new IndexItem() { Index_Name = parser["Index Name"], Index_Date = parseDate(parser["Index Date"]), Open_Index_Value = parser["Open Index Value"], High_Index_Value = parser["High Index Value"], Low_Index_Value = parser["Low Index Value"], Closing_Index_Value = parser["Closing Index Value"], Volume = parser["Volume"], }; currentdateindex = item.Index_Date; indexitems.Add(item); } converttotxt2(); } } else { MessageBox.Show("Please select Index File"); } }
private DataTable LerArquivoCSV(string caminhoParaLer) { const string formato_data = @"yyyyMMdd"; StringBuilder numeroContaConta = null; StringBuilder dataMovimentacao = null; StringBuilder numeroDocumento = null; StringBuilder historico = null; StringBuilder valorMovimentacao = null; StringBuilder tipoMovimentacao = null; DataTable tabelaRegistros = null; DataRow umRegistro = null; CultureInfo provider = null; DateTime dataSerieData; double valorSerieDouble = 0; numeroContaConta = new StringBuilder(); dataMovimentacao = new StringBuilder(); numeroDocumento = new StringBuilder(); historico = new StringBuilder(); valorMovimentacao = new StringBuilder(); tipoMovimentacao = new StringBuilder(); tabelaRegistros = CriarTabelaInputDoland(); provider = CultureInfo.InvariantCulture; using (var parser = new GenericParser()) { parser.SetDataSource(caminhoParaLer); parser.ColumnDelimiter = ';'; parser.FirstRowHasHeader = true; parser.MaxBufferSize = 4096; parser.MaxRows = 500000; valorSerieDouble = 0; while (parser.Read()) { numeroContaConta.Clear(); dataMovimentacao.Clear(); numeroDocumento.Clear(); historico.Clear(); valorMovimentacao.Clear(); tipoMovimentacao.Clear(); numeroContaConta.Append(parser[0]); dataMovimentacao.Append(parser[1]); numeroDocumento.Append(parser[2]); historico.Append(parser[3]); valorMovimentacao.Append(parser[4]); tipoMovimentacao.Append(parser[5]); umRegistro = tabelaRegistros.NewRow(); umRegistro[0] = numeroContaConta.ToString(); DateTime.TryParseExact(dataMovimentacao.ToString(), formato_data, provider, DateTimeStyles.None, out dataSerieData); umRegistro[1] = dataSerieData; umRegistro[2] = numeroDocumento.ToString(); umRegistro[3] = historico.ToString(); double.TryParse(valorMovimentacao.ToString(), out valorSerieDouble); umRegistro[4] = valorSerieDouble; umRegistro[5] = tipoMovimentacao.ToString(); tabelaRegistros.Rows.Add(umRegistro); } } //Utils.DataTableUtils.PrintDataTable(tabelaRegistros); return(tabelaRegistros); }
private void btn2_Click(object sender, EventArgs e) { //ArgumentParser argParser = new ArgumentParser(args); String dataSource = ofd1.FileName; //argParser["o"]; if (dataSource != null) { try { GenericParser parser = new GenericParser(); parser.SetDataSource(dataSource); parser.FirstRowHasHeader = true; parser.Read(); int columnCount = parser.ColumnCount; //Work out the names of the Accounts we need to sum List <string> ItemNames = new List <string>(); for (int i = 0; i < columnCount; i++) { ItemNames.Add(parser.GetColumnName(i)); } // //Work out the numbers we need to sum // List<Decimal> DecimalsToSum = new List<decimal>(); // for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++) // { // DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter])); // } // // List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>(); // List<Tuple<Decimal,Decimal,List<String>>> AbsSumsAndOps = new List<Tuple<Decimal,Decimal,List<String>>>(); // List<String> OpsSoFar = new List<string>(); // // callsCount = 0; // BuildPathsAndSumsTogether (SumsAndOps, AbsSumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount); // SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); // AbsSumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); //Work out the numbers we need to sum List <Decimal> DecimalsToSum = new List <decimal>(); for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++) { DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter])); } //List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>(); List <String> OpsSoFar = new List <string>(); callsCount = 0; maxAbsDiff = -9999; BuildPathsAndSumsTogetherMinimal(DecimalsToSum, OpsSoFar, 0, 0, columnCount); String stuff = "yarr"; Decimal outcome = maxAbsDiff; //BuildPathsAndSumsTogetherNoAbs (SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount);(SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount); //SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); // int countSuccess = 0; // textBox1.Text = ""; // foreach(Tuple<Decimal,List<string>> sumWithOps in SumsAndOps) { // if(sumWithOps.Item1 == 0) { // countSuccess++; // // for(int account = 0; account < ItemNames.Count; account++) { // textBox1.Text += (ItemNames[account].PadLeft(30) + ":" // + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4) // + "(" + sumWithOps.Item2[account] + ")" // + Environment.NewLine); // Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item2[account]); // } // textBox1.Text += ("Balances To".PadLeft(30) + ":" // + sumWithOps.Item1.ToString().PadLeft(15) // + Environment.NewLine + Environment.NewLine); // Console.WriteLine("Balances To" +":"+ sumWithOps.Item1); // Console.WriteLine(); // } // } // // if(countSuccess == 0) { // int n = 20; // //// textBox1.Text = "This combination of numbers has no possible way to balance. The " + n + " solutions closest to 0 follow." + Environment.NewLine; //// //// for(int closeResultPos = 0 ; closeResultPos < n || closeResultPos >= AbsSumsAndOps.Count; closeResultPos++) { //// Tuple<Decimal,Decimal,List<string>> sumWithOps = AbsSumsAndOps[closeResultPos]; //// for(int account = 0; account < ItemNames.Count; account++) { //// textBox1.Text += (ItemNames[account].PadLeft(30) + ":" //// + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4) //// + "(" + sumWithOps.Item3[account] + ")" //// + Environment.NewLine); //// Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item3[account]); //// } //// //// textBox1.Text += ("Balances To".PadLeft(30) + ":" //// + sumWithOps.Item2.ToString().PadLeft(15) //// + Environment.NewLine + Environment.NewLine); //// Console.WriteLine("Balances To" +":"+ sumWithOps.Item2); //// Console.WriteLine(); //// } // } } catch (Exception ex) { textBox1.Text = ex.Message; } } }
private void Opcion2() { int Fila = 0; ListaXml = new List <cp_XML_Documento_Info>(); try { using (GenericParser parser = new GenericParser()) { parser.SetDataSource(txtRutaXml.Text); parser.ColumnDelimiter = '\t'; parser.FirstRowHasHeader = false; parser.SkipStartingDataRows = 2; parser.MaxBufferSize = 4096; parser.MaxRows = 999999999; parser.TextQualifier = '\"'; cp_XML_Documento_Info info = new cp_XML_Documento_Info(); while (parser.Read()) { if (Fila == 104) { } if ((Fila % 2) == 0) { info = new cp_XML_Documento_Info { Tipo = parser[0].ToString().ToUpper(), CodDocumento = (parser[0].ToString().ToUpper() == "FACTURA" ? "01" : parser[0].ToString().ToUpper()), Comprobante = (parser[0].ToString().ToUpper() == "FACTURA" ? "01-" : (parser[0].ToString().ToUpper() + "-")) + parser[1].ToString(), emi_Ruc = parser[2].ToString(), emi_RazonSocial = parser[3].ToString(), FechaEmision = Convert.ToDateTime(parser[4].ToString()), ClaveAcceso = parser[8].ToString() }; var Doc = busXml.GetInfo(param.IdEmpresa, info.CodDocumento, info.Comprobante, info.emi_Ruc); if (Doc != null) { info.ValidacionD = Doc.ValidacionD; info.ValidacionR = Doc.ValidacionR; info.IdDocumento = Doc.IdDocumento; info.ValidacionC = Doc.ValidacionC; } Fila++; } else { info.Total = parser[0] == null ? 0 : Convert.ToDouble(parser[0]); ListaXml.Add(info); Fila++; } } } gcDetalle.DataSource = ListaXml; lblContador.Text = ListaXml.Count.ToString(); } catch (Exception ex) { gcDetalle.DataSource = ListaXml; lblContador.Text = ListaXml.Count.ToString(); Core.Erp.Info.Log_Exception.LoggingManager.Logger.Log(Core.Erp.Info.Log_Exception.LoggingCategory.Error, ex.Message); MessageBox.Show("Ha ocurrido un error en la fila: " + Fila.ToString(), param.Nombre_sistema, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void AbrirArquivoDelimitado() { if (comboBoxColumnDelimiter.SelectedItem.ToString() == "Outro") { if (string.IsNullOrEmpty(textBoxOtherDelimiter.Text)) { MessageBox.Show("Informe o delimitador desejado"); textBoxOtherDelimiter.Focus(); return; } } if (!string.IsNullOrEmpty(labelFileName.Text) && labelFileName.Text != "Nenhum arquivo selecionado") { dataGridView1.DataSource = null; dataGridView1.Refresh(); dataSet1.Tables.Clear(); using (GenericParser parser = new GenericParser()) { List <string> listHeader = new List <string>(); List <List <string> > listRows = new List <List <string> >(); parser.SetDataSource(labelFileName.Text); parser.TextFieldType = FieldType.Delimited; parser.ColumnDelimiter = GetColumnDelimiterChar(); parser.FirstRowHasHeader = checkBoxPrimeiraLinha.Checked; //parser.SkipStartingDataRows = 10; //parser.MaxBufferSize = 4096; //parser.MaxRows = 500; parser.TextQualifier = GetTextDelimiterChar(); while (parser.Read()) { List <string> listColsInRows = new List <string>(); if (listHeader.Count() == 0) { for (int i = 0; i < parser.ColumnCount; i++) { if (string.IsNullOrEmpty(parser.GetColumnName(i))) { listHeader.Add("Coluna " + (i + 1).ToString()); } else { listHeader.Add(parser.GetColumnName(i)); } } } else { if (listHeader.Count() < parser.ColumnCount) { for (int i = listHeader.Count(); i < parser.ColumnCount; i++) { if (string.IsNullOrEmpty(parser.GetColumnName(i))) { listHeader.Add("Coluna " + (i + 1).ToString()); } else { listHeader.Add(parser.GetColumnName(i)); } } } } if (listHeader.Count() > 0) { for (int i = 0; i < parser.ColumnCount; i++) { listColsInRows.Add(parser[i]); } } if (listColsInRows.Count() > 0) { listRows.Add(listColsInRows); } } DataTable dataTable = new DataTable(); foreach (string col in listHeader) { dataTable.Columns.Add(col); } foreach (List <string> row in listRows) { object[] values = new object[row.Count]; foreach (string col in row) { values[row.IndexOf(col)] = col; } dataTable.Rows.Add(values); } dataSet1.Tables.Add(dataTable); VerifyGridNumRowAndSetLabelReg(dataSet1.Tables[0].Rows.Count); dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = dataSet1.Tables[0]; dataGridView1.Focus(); } } else { if (!string.IsNullOrEmpty(labelFileName.Text) && labelFileName.Text != "Nenhum arquivo selecionado") { { MessageBox.Show("Selecione o arquivo texto"); } } } }
/// <summary> /// Parses a CSV file and loads the observables to the temporary Observables list /// </summary> /// <param name="filepath">The path to the CSV</param> private void ParseCSV(string filepath) { //If the path is empty, invalid or the file doesn't exist, stop if (string.IsNullOrWhiteSpace(filepath) || !File.Exists(filepath)) { return; } //Master try to catch any other uncaught issues try { //GenericParser written by AndrewRissing - https://github.com/AndrewRissing/GenericParsing using (GenericParser parser = new GenericParser()) { //Set the params of the parser parser.SetDataSource(filepath); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.TextQualifier = '\"'; //Start looping through the file line by line int line = 0; while (parser.Read()) { line++; try //Try per line { //Add the parsed columns to our dictionary of name/value pairs //Column order is expected to be as per strict template order //TODO: Make more flexible and allow different column orders Dictionary <string, string> values = new Dictionary <string, string>(); for (int i = 0; i < parser.ColumnCount; i++) { values.Add(parser.GetColumnName(i), parser[i]); } //Create a new observable object from the values read in this line ObservableObject newobs = null; string errors = GenerateObservable(values, out newobs); //If generating the observable gave any errors, add them to the error list if (errors != null) { AddError(errors); } else { //Otherwise add this new observable to our storage list Observables.Add(newobs); } } catch (Exception exep) { //This will likely occur if columns are missing or have been renamed etc in the template MessageBox.Show("There was an error parsing the CSV at line " + line + ".\r\n\r\n Please make sure it is in correct format - check the template provided."); return; } } } } catch (Exception exep) { //Should only occur due to unforseen IO errors like the file being in use etc MessageBox.Show("There was an error reading the CSV:\r\n" + exep.Message); return; } //Try to add the observables to the GUI try { //Clear the box for now and set the displaymember to the right property ObservablesListBox.Items.Clear(); ObservablesListBox.DisplayMember = "DisplayTitle"; //Add each observable to the listbox foreach (ObservableObject obs in Observables) { ObservablesListBox.Items.Add(obs); } //Refresh the listbox ObservablesListBox.Refresh(); } catch (Exception exep) { //Throw an exception here if there is an error updating the GUI, shouldn't happen under normal conditions MessageBox.Show("There was an error reading the CSV:\r\n" + exep.Message); return; } }
static void Main(string[] args) { data = new DataTable("What should I do today?"); data.Columns.Add("Weather"); data.Columns.Add("Time of the day"); data.Columns.Add("Homework"); data.Columns.Add("Bored"); data.Columns.Add("Activity"); using (GenericParser parser = new GenericParser()) { parser.SetDataSource("C:\\Users\\Marija\\source\\repos\\IntSys2\\WhatToDo4.csv"); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.TextQualifier = '\"'; while (parser.Read()) { data.Rows.Add(parser[0].ToString(), parser[1].ToString(), parser[2].ToString(), parser[3].ToString(), parser[4].ToString()); } } codebook = new Codification(data); DataTable symbols = codebook.Apply(data); int[][] inputs = symbols.ToArray <int>(data.Columns[0].ColumnName, data.Columns[1].ColumnName, data.Columns[2].ColumnName, data.Columns[3].ColumnName); int[] outputs = symbols.ToArray <int>(data.Columns[4].ColumnName); var id3learning = new ID3Learning() { new DecisionVariable(data.Columns[0].ColumnName, 3), new DecisionVariable(data.Columns[1].ColumnName, 2), new DecisionVariable(data.Columns[2].ColumnName, 2), new DecisionVariable(data.Columns[3].ColumnName, 2) }; DecisionTree id3Tree = id3learning.Learn(inputs, outputs); PrintTree(id3Tree.Root, "", id3Tree.Root.IsLeaf); int[] query = codebook.Transform(new string[, ] { { data.Columns[0].ColumnName, "Rainy" }, { data.Columns[1].ColumnName, "Daytime" }, { data.Columns[2].ColumnName, "No homework" }, { data.Columns[3].ColumnName, "Bored" } }); int predicted = id3Tree.Decide(query); string answer = codebook.Revert(data.Columns[4].ColumnName, predicted); Console.WriteLine("-------------------------"); Console.WriteLine("Id3 resenje: " + answer); Console.WriteLine("-------------------------"); //var c45learning = new C45Learning() //{ // new DecisionVariable(data.Columns[0].ColumnName, 3), // new DecisionVariable(data.Columns[1].ColumnName, 2), // new DecisionVariable(data.Columns[2].ColumnName, 2), // new DecisionVariable(data.Columns[3].ColumnName, 2) //}; //DecisionTree c45Tree = c45learning.Learn(inputs, outputs); //PrintTree(c45Tree.Root, "", c45Tree.Root.IsLeaf); //int c45predicted = c45Tree.Decide(query); //answer = codebook.Revert(data.Columns[4].ColumnName, c45predicted); //Console.WriteLine("-------------------------"); //Console.WriteLine("C45 resenje: " + answer); //Console.WriteLine("-------------------------"); Console.ReadLine(); }