简体   繁体   中英

Correct way parsing a text file

I have a text file that is coming is some predefined way. I don't have something like it's xsd, but the pattern can be seen.

for example it looks something like this:

[MyFIRSTPARAGRAPH]
NUM1 NUM2 NUM3 NUM4 NUM5 NUM6 NUM7 NUM8 NUM9 NUM10 NUM11
1 1 0.000 0.000 0.000 0 1 1 0 0 ""
2 2 22.800 0.000 0.000 0 1 1 0 0 ""
3 3 45.600 0.000 0.000 0 1 1 0 0 ""
4 4 68.400 0.000 0.000 0 1 1 0 0 ""
5 5 91.200 0.000 0.000 0 1 1 0 0 ""
6 6 0.000 32.800 0.000 0 1 1 0 0 ""
7 7 22.800 32.800 0.000 0 1 1 0 0 ""
8 8 45.600 32.800 0.000 0 1 1 0 0 ""
9 9 68.400 32.800 0.000 0 1 1 0 0 ""
10 10 91.200 32.800 0.000 0 1 1 0 0 "" 

A lot paragraphs separated by space lines.

Any saggestion what it the best what to parse files like this and to extract the values from the text.

My very first guess would be to do something like this:

using(var reader = GetStreamReader())
{
    bool justReadATag = false;
    string line = string.Empty;

    while((line = reader.ReadLine()) != null)
    {
        if(IsTag(line)) 
        {
            // do some work with the paragraph tag
            justReadATag = true;
        }else{
            string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if(justReadATag) 
            {
                // do some work with the column names
                justReadATag = false;    
            }else
            {
                // do some work with the cell values
            }
        }
    }
}

I would suggest read the complete file using File.ReadAllLines method. Now you can iterate all the lines one by one. Then for each line use String.Split(' ') to get the values which are separated by space in the line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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