繁体   English   中英

如何跳过txt文件块

[英]How to skip txt file chunks

如何跳过仅在红色框中读取文件才能继续在蓝色框中读取文件? 我需要对“ fileReader”进行哪些调整?

到目前为止,在SO用户的帮助下,我已经能够成功跳过前8行(第一个红色框)并读取文件的其余部分。 但是现在我只想阅读蓝色表示的部分。

我正在考虑为每个蓝色块制作一个方法。 基本上从跳过文件的前8行开始(如果它的第一个蓝框开始),而下一个蓝框大约跳过23行,但是结束文件阅读器是我遇到问题的地方。 根本不知道该使用什么。

在此处输入图片说明

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
            }
        } 
    }
}

尝试这样的事情:

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
}

正则表达式拆分很容易在任何空格上中断,直到下一个令牌为止。 正则表达式" +"表示匹配一个或多个空格。 当发现其他东西时,它将分裂。 根据您的示例,您只想解析以数字或日期开头的行,这应该可以完成。 请注意,我修剪了前导空格和尾随空格的行,以便您不会在任何空格上分割而得到空的字符串标记。

我可以看到您想要阅读的内容:

  1. 结尾线之间Numerics (可能的一个行之后)
  2. 直到以0Total开头的行(是零,对吗?);
  3. CURREN结尾的行之间
  4. 直到与线1作为所述行中的第一符号。

不应该很难。 逐行读取文件。 当(1)或(3)出现时,开始生成直到相应地(2)或(4)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM