Parsing a text file using C #

Looking for a good way to parse this text file, the values ​​are highlighted in yellow with C #. Each section is indicated by TERM #, which I forgot to highlight. Tried this:

string fileName = "ATMTerminalTotals.txt";
StreamReader sr = new StreamReader(fileName);
string[] delimiter = new string[] { " " };
while (!sr.EndOfStream)
{
     string[] lines = sr.ReadLine().Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
     foreach (string line in lines)
     {
         Console.WriteLine(line);
     }
}
Console.ReadLine();

It is safe to say that I read the lines correctly and remove the "spaces". Although, as a programming lover, I’m not sure of the right way to “know” for sure that I get the values ​​from this report that I need. Any advice?

+3
source share
5 answers

, , , ( tran) , , .

, , ,

, ..... , , id , ,

namespace TerminalTest
{
    class Program
    {
        public class TerminalReport
        {
            public string Word { get; set; }

            public int Denials { get; set; }

            public int Approvals { get; set; }

            public int Reversals { get; set; }

            public double Amount { get; set; }

            public int ON_US { get; set; }

            public int Alphalink { get; set; }

            public int Interchange { get; set; }

            public int Surcharged { get; set; }

            public static TerminalReport FromLine(string line)
            {
                TerminalReport report = new TerminalReport();
                report.Word = line.Substring(0, 11);
                line = line.Replace(report.Word, string.Empty).Trim();
                string[] split = line.Split(' ');
                int i = 0;
                // transaction summary
                report.Denials = int.Parse(split[i++]);
                report.Approvals = int.Parse(split[i++]);
                report.Reversals = int.Parse(split[i++]);
                report.Amount = double.Parse(split[i++]);
                // billing counts
                report.ON_US = int.Parse(split[i++]);
                report.Alphalink = int.Parse(split[i++]);
                report.Interchange = int.Parse(split[i++]);
                report.Surcharged = int.Parse(split[i++]);

                return report;
            }
        }

        public class TerminalPage
        {
            public int PageNumber { get; set; }

            public double TotalSurcharges { get; set; }

            public List<TerminalReport> Rows { get; set; }

            public TerminalPage(int num)
            {
                PageNumber = num;
                Rows = new List<TerminalReport>();
            }

            public int TotalDenials
            {
                get
                {
                    return rows.Sum(r => r.Denials);
                }
            }

            public int TotalApprovals
            {
                get
                {
                    return Rows.Sum(r => r.Approvals;
                }
            }

            public int TotalReversals
            {
                get
                {
                    return Rows.Sum(r => r.Reversals;
                }
            }

            public double TotalAmount
            {
                get
                {
                    return Rows.Sum(r => r.Amount);
                }
            }

            public int TotalON_US
            {
                get
                {
                    return Rows.Sum(r => r.ON_US);
                }
            }

            public int TotalAlphalink
            {
                get
                {
                     return Rows.Sum(r => r.Alphalink);
                }
            }

            public int TotalInterchange
            {
                get
                {
                     return Rows.Sum(r => r.Interchange);
                }
            }

            public int TotalSurcharged
            {
                get
                {
                     return Rows.Sum(r => r.Surcharged);
                }
            }
        }

        private static string CleanString(string text)
        {
            return Regex.Replace(text, @"\s+", " ").Replace(",", string.Empty).Trim();
        }

        private static List&lt;TerminalPage&gt; ParseData(string filename)
        {
            using (StreamReader sr = new StreamReader(File.OpenRead(filename)))
            {
                List<TerminalPage> pages = new List<TerminalPage>();

                int pageNumber = 1;
                TerminalPage page = null;
                bool parse = false;
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    line = CleanString(line);
                    if (line.StartsWith("TRAN TYPE"))
                    {
                        // get rid of the ----- line
                        sr.ReadLine();

                        parse = true;
                        if (page != null)
                        {
                            pages.Add(page);
                        }
                        page = new TerminalPage(pageNumber++);
                    }
                    else if (line.StartsWith("="))
                    {
                        parse = false;
                    }
                    else if (line.StartsWith("TOTAL SURCHARGES:"))
                    {
                        line = line.Replace("TOTAL SURCHARGES:", string.Empty).Trim();
                        page.TotalSurcharges = double.Parse(line);
                    }
                    else if (parse)
                    {
                        TerminalReport r = TerminalReport.FromLine(line);
                        page.Rows.Add(r);
                    }
                }
                if (page != null)
                {
                    pages.Add(page);
                }

                return pages;
            }
        }

        static void Main(string[] args)
        {
            string filename = @"C:\bftransactionsp.txt";
            List<TerminalPage> pages = ParseData(filename);

            foreach (TerminalPage page in pages)
            {
                Console.WriteLine("TotalSurcharges: {0}", page.TotalSurcharges);
                foreach (TerminalReport r in page.Rows)
                        Console.WriteLine(r.Approvals);

            }
        }
    }
}
+1

, . . 10 ( ) ... ,

dict["WDL FRM CHK"]["# DENIALS"] = 236

, , .


. ,

^WDL FRM CHK\s+(?<denials>[0-9,]+)\s+(?<approvals>[0-9,]+)$

m.Groups["approvals"]
+1

, StreamReader using:

using (StreamReader sr = new StreamReader(fileName))
{
    // do stuff
}

MSDN

+1

Given that it has a standard standard format, I would use regular expressions. You can check the source code to find out which line you are in, then an expression that will parse numbers and ignore spaces will most likely be easier than manually processing it.

0
source
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication3
{
  class Program
  {
    static void Main(string[] args)
    {
      Regex exp = new Regex(@"WDL FRM CHK(\s)+[1-9,]+(\s)+(?<approvals>[1-9,]+)(\s)+");
      string str = "WDL FRM CHK   236   1,854   45,465  123     3";
      Match match = exp.Match(str);

      if (match.Success)
      {
        Console.WriteLine("Approvals: " + match.Groups["approvals"].Value);
      }

      Console.ReadLine();
    }
  }
}

See the following article to analyze one of your numbers:

0
source

Source: https://habr.com/ru/post/1758626/


All Articles