public void Go()
        {
            var inputPath = IO.GetInputFilePath("TextFieldParseRegex.txt");
            var line = new string('=', 40);
            Console.WriteLine(line);
            using (var parser = new TextFieldParser(inputPath))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(";");
                parser.TrimWhiteSpace = true;
                parser.HasFieldsEnclosedInQuotes = true;

                while (parser.PeekChars(1) != null)
                {
                    Console.WriteLine("Line {0}: ", parser.LineNumber);
                    var cleanFieldRowCells = parser.ReadFields().Select(
                        f => f.Trim(new[] { ' ', '"' })).ToArray();
                    for (int i = 0; i < cleanFieldRowCells.Length; ++i)
                    {
                        Console.WriteLine(
                            "Field[{0}] = [{1}]", i, cleanFieldRowCells[i]
                        );
                    }
                    Console.WriteLine(line);
                }
            }
        }
        public void Go()
        {
            var inputPath = IO.GetInputFilePath("TextFieldParserKeepWhiteSpace.txt");

            var separator = new string('=', 40);
            Console.WriteLine(separator);
            // demo only - show the input lines read from a text file 
            var text = File.ReadAllText(inputPath);
            var lines = text.Split(
                new string[] { Environment.NewLine }, 
                StringSplitOptions.None
            );

            using (var textReader = new StringReader(text))
            {
                using (var parser = new TextFieldParser(textReader))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.TrimWhiteSpace = true;
                    parser.HasFieldsEnclosedInQuotes = false;
                    // remove double quotes, since HasFieldsEnclosedInQuotes is false
                    var regex = new Regex(@"
                    # match double quote 
                    \""    
                    # if not immediately followed by a double quote
                    (?!\"")
                    ",
                        RegexOptions.IgnorePatternWhitespace
                    );

                    var rowStart = 0;
                    while (parser.PeekChars(1) != null)
                    {
                        Console.WriteLine(
                            "row {0}: {1}", parser.LineNumber, lines[rowStart]
                        );
                        var fields = parser.ReadFields();
                        for (int i = 0; i < fields.Length; ++i)
                        {
                            Console.WriteLine(
                                "parsed field[{0}] = [{1}]", i,
                                regex.Replace(fields[i], "")
                            );
                        }
                        ++rowStart;
                        Console.WriteLine(separator);
                    }
                }
            }


        }
Example #3
0
 private void btnParseTextFiles_Click(object sender, EventArgs e)
 {
     using (TextFieldParser myReader = new TextFieldParser("test.txt"))
     {
         // 定义三种格式之各栏的宽度与分隔字符。
         int[] FirstFormat = { 5, 10, -1 };
         int[] SecondFormat = { 6, 10, 17, -1 };
         string[] ThirdFormat = { "," };
         this.DataGridView1.Rows.Clear();
         this.DataGridView2.Rows.Clear();
         this.DataGridView3.Rows.Clear();
         string[] CurrentRow;
         while (!myReader.EndOfData)
         {
             try
             {
                 string RowType = myReader.PeekChars(2);
                 switch (RowType)
                 {
                     case "CK":
                         myReader.TextFieldType = FieldType.FixedWidth;
                         myReader.FieldWidths = FirstFormat; // 或是 myReader.SetFieldWidths(FirstFormat);
                         CurrentRow = myReader.ReadFields();
                         this.DataGridView1.Rows.Add(CurrentRow);
                         break;
                     case "PB":
                         myReader.TextFieldType = FieldType.FixedWidth;
                         myReader.FieldWidths = SecondFormat; // 或是 myReader.SetFieldWidths(SecondFormat);
                         CurrentRow = myReader.ReadFields();
                         this.DataGridView2.Rows.Add(CurrentRow);
                         break;
                     case "SP":
                         myReader.TextFieldType = FieldType.Delimited;
                         myReader.Delimiters = ThirdFormat; // 或是 myReader.SetDelimiters(ThirdFormat);
                         CurrentRow = myReader.ReadFields();
                         this.DataGridView3.Rows.Add(CurrentRow);
                         break;
                 }
             }
             catch (MalformedLineException ex)
             {
                 MessageBox.Show("行 " + ex.Message + " 是无效的。略过。");
             }
         }
         // 排序各个 DataGridView 控件的内容。
         DataGridView1.Sort(DataGridView1.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
         DataGridView2.Sort(DataGridView2.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
         DataGridView3.Sort(DataGridView3.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
     }
 }
Example #4
0
        static bool VerifyZipAndRegion()
        {
            //////////////////////////////////////////////////////////////////////////////////////////
            /////////////////////////////////////////////////////////////////// INITIALIZING VARIABLES
            //////////////////////////////////////////////////////////////////////////////////////////
            string[] aryTableParsedLine;
            string[] aryDataParsedLine;
            string strDataLine;
            string strTableLine;
            string strEditedDataFile = strInputDataName.ToUpper().Replace(".CSV", "_EDITED.CSV");
            string strCurrentZip;
            string strDataZipAndRegion;
            string strTableZipAndRegion;
            int iZipFieldIndex = 0;
            int iDataFields;
            bool bolFoundRegionMatch;
            int iInputRecords;
            int iEditedRecords;
            int iMismatchRecords;
            bool bolZipFieldFound = false;
            strRegionErrorsRemoved = strWorkingJobFolder + strJobNumber + " - Region Mismatches Removed.csv";

            StreamReader streamInitialFileScan = new StreamReader(strInputDataName);
            StreamReader streamTableFile = new StreamReader(strZipToRegionTable);
            StreamWriter streamEditedDataFile = new StreamWriter(strEditedDataFile);
            StreamWriter streamRegionMismatches = new StreamWriter(strRegionErrorsRemoved);

            TextFieldParser parseDataFile = new TextFieldParser(strInputDataName);
            parseDataFile.TextFieldType = FieldType.Delimited;
            parseDataFile.SetDelimiters(",");

            try
            {
                //////////////////////////////////////////////////////////////////////////////////////////
                ////////////////////////// DETERMINING WHICH FIELD IN THE INPUT DATA CONTAINS THE ZIP CODE
                //////////////////////////////////////////////////////////////////////////////////////////
                strDataLine = streamInitialFileScan.ReadLine();
                aryDataParsedLine = strDataLine.Split(',');
                iDataFields = aryDataParsedLine.Length;

                for (int j = 0; j < iDataFields; j++)
                {
                    if (aryDataParsedLine[j].ToString().ToUpper().Contains("ZIP"))
                    {
                        bolZipFieldFound = true;
                        streamEditedDataFile.WriteLine(strDataLine);
                        iZipFieldIndex = j;

                        break;
                    }
                }

                streamInitialFileScan.Close();
                streamInitialFileScan.Dispose();

                // Verifying that a zip code field exists in the input data file.
                if (!bolZipFieldFound)
                {
                    LogFile("A Zip field is not included in the input data file.", true);
                    return false;
                }

                //////////////////////////////////////////////////////////////////////////////////////////
                ///////////////////////////////////////// TESTING EACH RECORD AGAINST THE ZIP-REGION TABLE
                //////////////////////////////////////////////////////////////////////////////////////////
                while (!parseDataFile.EndOfData)
                {
                    bolFoundRegionMatch = false;

                    strDataLine = parseDataFile.PeekChars(Int32.MaxValue);
                    aryDataParsedLine = parseDataFile.ReadFields();

                    // Capturing the zip and region combination from the current record.
                    strCurrentZip = aryDataParsedLine[iZipFieldIndex].ToString().Trim();

                    if (strCurrentZip.Length > 5)
                    {
                        strCurrentZip = strCurrentZip.Substring(0,5);
                    }

                    strDataZipAndRegion = strCurrentZip + strRegionCode;

                    // Looping through the Zip and Region Lookup Table to see if a zip is within a valid region.
                    while (!streamTableFile.EndOfStream)
                    {
                        strTableLine = streamTableFile.ReadLine();
                        aryTableParsedLine = strTableLine.Split(',');

                        strTableZipAndRegion = aryTableParsedLine[0].ToString() + aryTableParsedLine[2].ToString().ToUpper().Trim();

                        if (strDataZipAndRegion == strTableZipAndRegion)
                        {
                            bolFoundRegionMatch = true;

                            break;
                        }
                    }

                    if (bolFoundRegionMatch)
                    {
                        streamEditedDataFile.WriteLine(strDataLine);
                    }
                    else
                    {
                        streamRegionMismatches.WriteLine(strDataLine);
                    }

                    streamTableFile.DiscardBufferedData();
                    streamTableFile.BaseStream.Position = 0;
                }
            }
            catch (Exception exception)
            {
                LogFile(exception.ToString(), true);
                return false;
            }
            finally
            {
                streamEditedDataFile.Close();
                streamEditedDataFile.Dispose();
                streamTableFile.Close();
                streamTableFile.Dispose();
                streamRegionMismatches.Close();
                streamRegionMismatches.Dispose();
                parseDataFile.Close();
                parseDataFile.Dispose();
            }

            //////////////////////////////////////////////////////////////////////////////////////////
            /////////////////////////////////// DETERMINING IF THE HIGHMARK THRESHOLD HAS BEEN REACHED
            //////////////////////////////////////////////////////////////////////////////////////////
            try
            {
                // Calculating total number of input records.
                iInputRecords = File.ReadAllLines(strInputDataName).Length - 1;

                // Calculating total number of edited records.
                iEditedRecords = File.ReadAllLines(strEditedDataFile).Length - 1;

                // Calculating total number of mismatch records.
                iMismatchRecords = File.ReadAllLines(strRegionErrorsRemoved).Length - 1;

                if ((((decimal)iEditedRecords / (decimal)iInputRecords) * 100) <= iZipToRegionMismatchThreshold)
                {
                    bolRegionMismatchThresholdReached = true;

                    SendEmail("HIGHMARK");
                    LogFile("At least " + (100 - iZipToRegionMismatchThreshold).ToString() + "% of records submitted for processing were removed as Region-Zip mismatches.", true);
                    return false;
                }
                else
                {
                    if (iMismatchRecords > 1)
                    {
                        SendEmail("HIGHMARK");
                    }
                }
            }
            catch (Exception exception)
            {
                LogFile(exception.ToString(), true);
                return false;
            }

            strInputDataName = strEditedDataFile;
            return true;
        }
        public void PeekTest()
        {
            using (StringReader reader = new StringReader ("abcd" + Constants.vbNewLine + "efgh" + Constants.vbNewLine + "'comment" + Constants.vbNewLine + "after comment" + Constants.vbNewLine))
            using (TextFieldParser t = new TextFieldParser (reader)) {
                Assert.AreEqual ("a", t.PeekChars (1), "#01");
                Assert.AreEqual ("a", t.PeekChars (1), "#02");
                Assert.AreEqual ("ab", t.PeekChars (2), "#03");
                Assert.AreEqual ("abcd", t.PeekChars (10), "#04");
                Assert.AreEqual ("abcd", t.ReadLine (), "#05");
                Assert.AreEqual ("ef", t.PeekChars (2), "#06");

                try {
                    t.PeekChars (0);
                } catch (ArgumentException ex){
                    Helper.RemoveWarning (ex);
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#07 - Expected 'ArgumentException'");
                }

                try {
                    t.PeekChars (-1);
                } catch (ArgumentException ex) {
                    Helper.RemoveWarning (ex);
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#08 - Expected 'ArgumentException'");
                }

                Assert.AreEqual ("efgh", t.PeekChars (10), "#09");
                Assert.AreEqual ("efgh", t.ReadLine (), "#10");
                t.CommentTokens = new string [] {"'"};
                Assert.AreEqual ("afte", t.PeekChars (4), "#11");
                Assert.AreEqual ("'comment", t.ReadLine (), "#12");
                Assert.AreEqual ("af", t.PeekChars (2), "#13");
                Assert.AreEqual ("after comment", t.ReadLine (), "#14");
            }
        }
Example #6
0
        private void StartBenchmark(string args, string path)
        {
            StartGame(args, path);

            // Parse the csv file generated by TF2.

            var results = new FileInfo(path + @"\tf\sourcebench.csv");
            if (!results.Exists)
            {
                WorkerThread.ReportProgress(0, "Benchmark results file not found.");
                return;
            }

            using (var parser = new TextFieldParser(results.FullName))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                parser.HasFieldsEnclosedInQuotes = false;
                parser.TrimWhiteSpace = true;

                int lineCount = File.ReadLines(results.FullName).Count();

                // Skip the first header line of the file.
                parser.ReadLine();

                while (parser.PeekChars(1) != null)
                {
                    var li = new ListViewItem();

                    // Read every other line if benchmarking twice per command.
                    if (cb_runtwice.Checked && lineCount > 2 && parser.LineNumber % 2 == 0)
                    {
                        parser.ReadLine();
                        continue;
                    }

                    string[] row = parser.ReadFields();
                    for (int i = 0; i < row.Length; i++)
                    {
                        switch(i)
                        {
                            case 0:
                                li.Text = row[i];
                                break;
                            case 1: case 2: case 3: case 4: case 12: case 23:
                                li.SubItems.Add(row[i]);
                                break;
                        }
                    }

                    // Add the results to the listview.
                    if (lv_results.InvokeRequired)
                    {
                        lv_results.Invoke(new MethodInvoker(delegate
                        {
                            lv_results.Items.Add(li);
                        }));
                    }
                    else
                        lv_results.Items.Add(li);
                }
            }
        }