开发者

Parsing text file using C#

Looking for a good way to parse out of this text file, the values highlighted with the yellow boxes using C#. Each section is delineated by a TERM # which I forgot to highlight. Tried this:

string fileName = "ATMTerminalTotals.txt";
StreamReader sr = new StreamReader(fileNam开发者_JS百科e);
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();

Safe to say I am reading lines correctly and removing "white spaces." Although, as an amateur to programming, not sure of a valid way to accurately "know" that I am getting the values from this report that I need. Any advice?


i've tested this with a very simple program to parse the given file, basically i've created two basic classes, a page class holding a collection of terminal report class (the tran type rows) these rows maybe even can be represented as transaction and a billing class too

first parsed the data, setting the parameters needed and lastly just accessing the properties

just rushed it to be as simple as possible, no error handling etc... its just to give you a sense of how id start solving these kind of tasks, hope it helps

Adam

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);

            }
        }
    }
}


I'm not sure I'd split it by spaces actually.. the textfile looks like its split into columns. You might want to read like 10 chars (or whatever the width of the column is) at a time... and I'd parse the whole file into a dictionary so you get entries like

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

then you can easily retrieve the values you want from there, and if you ever need more values in the future, you've got them.


Alternatively, you can use regexs. You can grab the first value with a regex like

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

using

m.Groups["approvals"]


anyway I recommend you to wrap your StreamReader with using block:

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

Read more on MSDN


Given that it seems to have a standard, regular format, I would use regular expressions. You can check the starting code to figure out what row you're on, then an expression that will parse out the numbers and ignore whitespace will, very likely, be easier than handling it manually.


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();
    }
  }
}

Apdated from the following article to parse one of your numbers:

  • How to match a pattern by using regular expressions and Visual C#
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜