简体   繁体   English

如何逐块处理此文本?

[英]How to process this text block by block?

I want to process the data block by block separately 我想单独处理数据

Here is the text: 这是文本:

[Global] [全球]
asd ASD
dsa DSA
akl AKL
ASd ASD

[Test2] [Test2的]
bnmnb bnmnb
hkhjk hkhjk
tzutzi tzutzi
Tzutzi Tzutzi
Tzitzi Tzitzi

[Test3] [Test3的]
5675 5675
46546 46546
464 464
564 564
56456 56456
45645654 45645654
4565464 4565464

[other] [其他]
sdfsd sdfsd
dsf DSF
sdf SDF
dsfs 深海渔业

And first I want the first block and process it than the second... etc.. 首先,我要第一个块比第二个块处理...等等。

private void textprocessing(string filename)
{
    using (StreamReader sr1 = new StreamReader(filename))
    {
        string linetemp = "";
        bool found = false;
        int index = 0;

        while ((linetemp=sr1.ReadLine())!=null)
        {
            if (found==true)
            {
                MessageBox.Show(linetemp);
                break;   
            }

            if (linetemp.Contains("["))
            {
                found = true;
            }
            else
            {
                found = false;
            }                                                             
        }                                    
    }          
}

You can use string.Split() to split your strings based on "[" then split that based on newlines. 您可以使用string.Split()根据“ [”分割字符串,然后根据换行符分割字符串。 THen you check for the presence of "]" 然后检查是否存在“]”

void Main()
{
    string txt = @"[Global]
asd
dsa
akl
ASd

[Test2]
bnmnb
hkhjk
tzutzi
Tzutzi
Tzitzi

[Test3]
5675
46546
464
564
56456
45645654
4565464

[other]
sdfsd
dsf
sdf
dsfs";

    string[] split = txt.Split('[');
    foreach(var s in split)
    {
        var subsplits = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        Console.WriteLine(subsplits[0]);
        foreach(var ss in subsplits)
        {
            if(!ss.Contains("]"))
                Console.WriteLine(ss);
        }
    }
}

This outputs 这个输出

asd
dsa
akl
ASd


bnmnb
hkhjk
tzutzi
Tzutzi
Tzitzi


5675
46546
464
564
56456
45645654
4565464


sdfsd
dsf
sdf
dsfs

You could add an aditional check to check if it's a blank line and ignore it. 您可以添加一个附加检查以检查它是否为空行并忽略它。

Here's one approach: 这是一种方法:

private void ReadFile()
{
    //load all  lines
    var lines = File.ReadAllLines(@"c:\temp\file.txt").ToList().;
    //remove empty lines
    lines = lines.Where(l => l.Trim().Length > 0).ToList();
    //mark indexes where sections start
    var sectionIndexes = lines
        .Where(l => l.StartsWith("[") && l.EndsWith("]"))
        .Select(l => lines.IndexOf(l)).ToList();

    //now make list of tuples. Each tuple contains start of section (Item1) and last line of section (Item2)
    var sections = Enumerable.Zip(sectionIndexes, sectionIndexes.Skip(1), (a, b) => new Tuple<int, int>(a, b-1)).ToList();

    //for each tuple (each section)
    foreach (var item in sections)
    {
        //process section name (line with raound brackets
        ProcessSection(lines[item.Item1], lines.Where(l => lines.IndexOf(l) > item.Item1 && lines.IndexOf(l) <= item.Item2));
    }
}

private void ProcessSection(string sectionName, IEnumerable<string> lines)
{
    Console.WriteLine("this is section {0} with following lines: {1}", sectionName, string.Join(", ", lines.ToArray()));
}

output of ProcessSection method would be: ProcessSection方法的输出为:

this is section [Global] with following lines: asd, dsa, akl, ASd
this is section [Test2] with following lines: bnmnb, hkhjk, tzutzi, Tzutzi, Tzitzi
this is section [Test3] with following lines: 5675, 46546, 464, 564, 56456, 45645654, 4565464

This is pretty quick and dirty solution, but it would suffice if file you're reading is small. 这是一个非常快捷和肮脏的解决方案,但是如果您正在读取的文件很小,就足够了。

If you have additional questions, feel free to ask. 如果您还有其他问题,请随时提问。

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

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