使用 C# 解析文本文件
寻找一种解析此文本文件的好方法,使用 C# 用黄色框突出显示值。每个部分都由一个我忘记突出显示的术语# 来描述。尝试过这个:
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();
可以肯定地说我正在正确阅读行并删除“空格”。尽管作为编程的业余爱好者,不确定是否有有效的方法来准确地“知道”我正在从这份报告中获取我需要的值。有什么建议吗?
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(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();
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已经用一个非常简单的程序来测试这个来解析给定的文件,
基本上我创建了两个基本类,一个页面类包含终端报告类的集合(tran 类型行)
这些行甚至可以表示为事务和计费类,
首先解析数据,设置所需的参数,最后只是访问属性,
只是让它尽可能简单,没有错误处理等...它只是为了给出你知道我是如何开始解决这类任务的,希望它能帮助
亚当
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
我不确定我实际上会用空格将其分割。文本文件看起来像是分割成列。您可能想一次读取 10 个字符(或任何列的宽度)...我会将整个文件解析到字典中,这样您就可以获得像这样的条目
,然后您可以轻松地从中检索您想要的值在那里,如果你将来需要更多的价值,你已经得到了它们。
或者,您可以使用正则表达式。您可以使用正则表达式获取第一个值,例如
使用
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
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
using
无论如何,我建议您使用
using
块包装您的StreamReader
:阅读 MSDN
anyway I recommend you to wrap your
StreamReader
withusing
block: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.
改编自以下文章以解析您的一个号码:
Apdated from the following article to parse one of your numbers: