[英]How to skip txt file chunks
How do I skip reading the file at the red boxes only to continue reading the file at the blue boxes? 如何跳过仅在红色框中读取文件才能继续在蓝色框中读取文件? What adjustments would I need to make to 'fileReader'? 我需要对“ fileReader”进行哪些调整?
So far, with the help of SO users, I've been able to successfully skip the first 8 lines (first red box) and read the rest of the file. 到目前为止,在SO用户的帮助下,我已经能够成功跳过前8行(第一个红色框)并读取文件的其余部分。 But now I want to read ONLY the parts indicated in blue. 但是现在我只想阅读蓝色表示的部分。
I'm thinking of making a method for each chunk in blue. 我正在考虑为每个蓝色块制作一个方法。 Basically start it by skipping first 8 lines of file if its first blue box, about 23 for the next blue box but ending the file reader is where I'm having problems. 基本上从跳过文件的前8行开始(如果它的第一个蓝框开始),而下一个蓝框大约跳过23行,但是结束文件阅读器是我遇到问题的地方。 Simply don't know what to use. 根本不知道该使用什么。
private void button1_Click(object sender, EventArgs e)
{
// Reading/Inputing column values
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] lines = File.ReadAllLines(ofd.FileName).Skip(8).ToArray();
textBox1.Lines = lines;
int[] pos = new int[3] {0, 6, 18}; //setlen&pos to read specific colmn vals
int[] len = new int[3] {6, 12, 28}; // only doing 3 columns right now
foreach (string line in textBox1.Lines)
{
for (int j = 0; j < 3; j++) // 3 columns
{
val[j] = line.Substring(pos[j], len[j]).Trim();
list.Add(val[j]); // column values stored in list
}
}
}
}
Try something like this: 尝试这样的事情:
using System.Text.RegularExpressions; //add this using
foreach (string line in lines)
{
string[] tokens = Regex.Split(line.Trim(), " +");
int seq = 0;
DateTime dt;
if(tokens.Length > 0 && int.TryParse(tokens[0], out seq))
{
// parse this line - 1st type
}
else if (tokens.Length > 0 && DateTime.TryParse(tokens[0], out dt))
{
// parse this line - 2nd type
}
// else - don't parse the line
}
The Regex split is handy to break on any spaces till the next token. 正则表达式拆分很容易在任何空格上中断,直到下一个令牌为止。 The Regex " +"
means match one or more spaces. 正则表达式" +"
表示匹配一个或多个空格。 It splits when it finds something else. 当发现其他东西时,它将分裂。 Based on your example, you only want to parse lines that begin with a number or a date, which this should accomplish. 根据您的示例,您只想解析以数字或日期开头的行,这应该可以完成。 Note that I trimmed the line of leading and trailing spaces so that you don't split on any of those and get empty string tokens. 请注意,我修剪了前导空格和尾随空格的行,以便您不会在任何空格上分割而得到空的字符串标记。
I can see what you want to read anything what: 我可以看到您想要阅读的内容:
Numerics
(possible one line after) 结尾线之间Numerics
(可能的一个行之后) 0Total
(is that zero, right?); 直到以0Total
开头的行(是零,对吗?); CURREN
以CURREN
结尾的行之间 1
as first symbol in the row. 直到与线1
作为所述行中的第一符号。 Shouldn't be hard. 不应该很难。 Read file by line. 逐行读取文件。 When (1) or (3) occurs, start generating until (2) or (4) correspondingly. 当(1)或(3)出现时,开始生成直到相应地(2)或(4)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.