尝试编写 c# 程序来解析包含由方括号中的标题分隔的数据的文本文件

发布于 2024-11-06 07:31:57 字数 910 浏览 5 评论 0原文

我正在编写一个程序来解析一个特定的文本文件,其中包含稍后要处理的数据。每个部分都由方括号中的标头分隔,我想知道如何使用标头作为数组的名称将每个段放入数组中。下面是文本文件开头的示例。我设置了一个表单,让您选择要处理的文件,并且还设置了一种在循环中使用 objReader.ReadLine 逐行处理它的方法

[Params]
Version=106
Monitor=34
SMode=111111100
Date=20090725
StartTime=13:56:44.0
Length=00:24:30.5
Interval=1
Upper1=0
Lower1=0
Upper2=0
Lower2=0
Upper3=0
Lower3=0
Timer1=00:00:00.0
Timer2=00:00:00.0
Timer3=00:00:00.0
ActiveLimit=0
MaxHR=180
RestHR=70
StartDelay=0
VO2max=51
Weight=0

[Note]
TT Warm Up

[IntTimes]
00:24:30.5  140 83  154 174
0   0   0   41  112 33
0   0   0   0   0
0   12080   0   280 0   0
0   0   0   0   0   0

[IntNotes]

[ExtraData]

[Summary-123]
1470    0   1470    0   0   0
180 0   0   70
1470    0   1470    0   0   0
180 0   0   70
0   0   0   0   0   0
180 0   0   70
0   1470

[Summary-TH]
1470    0   1470    0   0   0
180 0   0   70
0   1470

[HRZones]
180
162
144
126
108
90
0
0
0
0
0  

Im writting a program that parses a specific text file that has data in it that im going to prcess later. Each part is seperated by a header in square brackets i was wondering how i could put each segment into an array using the header as a name for the array. Below is an example of the begining of the text file. ive set up a form that lets you choose a file you want to process and ive also set up a way of processing it line by line using objReader.ReadLine in a loop

[Params]
Version=106
Monitor=34
SMode=111111100
Date=20090725
StartTime=13:56:44.0
Length=00:24:30.5
Interval=1
Upper1=0
Lower1=0
Upper2=0
Lower2=0
Upper3=0
Lower3=0
Timer1=00:00:00.0
Timer2=00:00:00.0
Timer3=00:00:00.0
ActiveLimit=0
MaxHR=180
RestHR=70
StartDelay=0
VO2max=51
Weight=0

[Note]
TT Warm Up

[IntTimes]
00:24:30.5  140 83  154 174
0   0   0   41  112 33
0   0   0   0   0
0   12080   0   280 0   0
0   0   0   0   0   0

[IntNotes]

[ExtraData]

[Summary-123]
1470    0   1470    0   0   0
180 0   0   70
1470    0   1470    0   0   0
180 0   0   70
0   0   0   0   0   0
180 0   0   70
0   1470

[Summary-TH]
1470    0   1470    0   0   0
180 0   0   70
0   1470

[HRZones]
180
162
144
126
108
90
0
0
0
0
0  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伊面 2024-11-13 07:31:57

您可以使用类似这样的模式。

List<String> paramsList;
List<String> noteList;
List<String> tempList;

while (line = objReader.ReadLine()) {
    if (line.StartsWith("[")) {

        // start new array
        if (line.Equals("[Params]"))
            tempList = paramsList = new List<String>();
        else if (line.Equals("[Note]"))
            tempList = noteList = new List<String>();

        // etc.

    } else if (String.IsNullOrEmpty(line)) {

        // ignore, end of array

    } else {

        // add element to array
        tempList.Add(line);

    }
}

// now use paramsList, noteList, etc. as needed

我不太确定您如何使用标头作为数组的名称。所有文件的标头是否始终相同?您不能根据字符串动态分配变量的名称,如果您需要在以后的处理中使用它,您也不想这样做。

You could use a pattern something like this.

List<String> paramsList;
List<String> noteList;
List<String> tempList;

while (line = objReader.ReadLine()) {
    if (line.StartsWith("[")) {

        // start new array
        if (line.Equals("[Params]"))
            tempList = paramsList = new List<String>();
        else if (line.Equals("[Note]"))
            tempList = noteList = new List<String>();

        // etc.

    } else if (String.IsNullOrEmpty(line)) {

        // ignore, end of array

    } else {

        // add element to array
        tempList.Add(line);

    }
}

// now use paramsList, noteList, etc. as needed

I'm not quite sure how you mean to use the header as a name for the array. Are the headers always the same for all of the files? You can't dynamically allocate the name of a variable based on a string, nor would you want to if you need to use it in later processing.

扛刀软妹 2024-11-13 07:31:57

我会将其解析为字典:

    Dictionary<string, List<string>> d = new Dictionary<string, List<string>>();

    using (StreamReader reader = new StreamReader("filename"))
    {
        string token = null;
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            if (line.StartsWith("["))
                d[token] = new List<string>();
            else
                d[token].Add(line);
        }
    }

如果有重复的标记,上述方法还将附加数据行。

I'd parse it into a dictionary:

    Dictionary<string, List<string>> d = new Dictionary<string, List<string>>();

    using (StreamReader reader = new StreamReader("filename"))
    {
        string token = null;
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            if (line.StartsWith("["))
                d[token] = new List<string>();
            else
                d[token].Add(line);
        }
    }

The above approach will also append data lines if you have duplicate tokens.

め七分饶幸 2024-11-13 07:31:57

由于此文件格式超出了普通的 INI 文件(请参阅 Nicholas Carey 的答案中 David Yaw 的评论),我将使用由解析器生成器创建的适当解析器。我选择的工具是 GOLD Parser Builder,但任何其他解析器生成器也可以。

Since this file format goes beyond a normal INI file (see comment by David Yaw in Nicholas Carey's answer) I'd use a proper parser created by a parser generator. My tool of choice is the GOLD Parser Builder, but any other parser generator may do just as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文