【问题标题】:Reading Multi-Lines from a Text File从文本文件中读取多行
【发布时间】:2014-01-30 02:03:01
【问题描述】:

我正在尝试读取文本文件中的多行。它看起来像这样:

#############################################
B_ATTR 'MILLING_ORIGIN' 'MILLING 0 0.0 0'  3.635 0.405
B_ATTR 'DRILL_ORIGIN' ''  0.0 0.0
B_ATTR 'BOARD_ROUTING_OUTLINE' ''  0.0192 0.0885 0.0862 0.0215 3.84915 0.0215 - 
3.9162 0.0885 3.9162 0.7018 3.8491 0.7689 3.6793 0.7689 3.65335 0.7715 3.6274 0.77935 - 
3.60355 0.7921 3.5826 0.8093 3.5654 0.83025 3.55265 0.8541 3.5448 0.88 3.5422 0.906 - 
3.5422 5.62345 3.54335 5.6408 3.54685 5.6585 3.55265 5.67565 3.56065 5.69185 3.57065 5.7069 - 
3.58255 5.7205 3.59615 5.73245 3.61065 5.7421 3.9162 5.91855 3.9162 6.2136 3.8491 6.2807 - 
0.0862 6.2807 0.0192 6.21365 0.0192 5.91855 0.32465 5.7421 0.33915 5.7324 0.3527 5.72045 - 
0.36465 5.7069 0.3747 5.6919 0.3827 5.67565 0.3885 5.65855 0.39205 5.64085 0.3932 5.62345 - 
0.3932 0.906 0.3906 0.88 0.38275 0.8541 0.37 0.83025 0.3528 0.8093 0.33185 0.7921 - 
0.308 0.77935 0.28205 0.7715 0.2561 0.7689 0.0862 0.7689 0.0192 0.7018
B_ATTR 'BOARD_PLACEMENT_OUTLINE' ''  0.0192 0.0885 0.0862 0.0215 3.84915 0.0215 - 
3.9162 0.0885 3.9162 0.7018 3.8491 0.7689 3.6793 0.7689 3.65335 0.7715 3.6274 0.77935 - 
3.60355 0.7921 3.5826 0.8093 3.5654 0.83025 3.55265 0.8541 3.5448 0.88 3.5422 0.906 - 
3.5422 5.62345 3.54335 5.6408 3.54685 5.6585 3.55265 5.67565 3.56065 5.69185 3

读取将从以“B_ATTR 'BOARD_ROUTING_OUTLINE'”开头的行开始,以“B_ATTR 'BOARD_PLACEMENT_OUTLINE'”开头的行结束。我写了这段代码,但它不起作用。我无法摆脱一些空格。

StreamReader reader = new StreamReader("neutral.txt");

        string lines = reader.ReadToEnd(); //Reading all lines.

        //Takes the lines I want.
        lines = lines.Substring(lines.IndexOf("B_ATTR 'BOARD_ROUTING_OUTLINE'"), lines.IndexOf("B_ATTR 'BOARD_PLACEMENT_OUTLINE'") - lines.IndexOf("B_ATTR 'BOARD_ROUTING_OUTLINE'"));

        //if the line contains "-" , replaces with " "(space).
        while (lines.Contains("-"))
            lines = Regex.Replace(lines, @"\-", " ");

        //if the line contains "  "(two spaces), replaces with " "(one space).
        while (lines.Contains("  "))
            lines = Regex.Replace(lines, @"\  ", " ");

        string[] items = lines.Split(' ');

        List<string> list = new List<string>();

        //Places all of the elements of items to list.
        foreach (var element in items)
            list.Add(element);


        //if the list contains any space , removes it.
        while (list.Contains(" "))
            list.Remove(" ");


        //Removes the elements I don't want.
        list.Remove("B_ATTR");
        list.Remove("'BOARD_ROUTING_OUTLINE'");
        list.Remove("''");
        list.Remove("B_ATTR");
        list.Remove("'BOARD_PLACEMENT_OUTLINE'");           

        string[,] array = new string[50, 2];

        string[] array2 = list.ToArray(); //Turns the list to array.

        int counter = 0;


        //Makes a rectangular array with 50 rows and 2 columns.
        for (int a = 0; a < 50; a++)
            for (int b = 0; b < 2; b++)
            {
                array[a, b] = array2[counter];
                counter++;                    
            }

我实际上使用正则表达式来消除空格,但我无法消除行之间的空格以及发生在数组中的空格。

你有什么建议吗?我做错了吗?

【问题讨论】:

  • 使用File.ReadLinesFile.ReadAllLines会更方便
  • 使用 File.ReadAllLines,您可以首先准备好数组。你会得到indexof你的两个文本,并在两者之间取元素。
  • 我试过了。我用 File.ReadAllLines 阅读它并使用“join”将它们变成一个行字符串。然后我使用“子字符串”并获得我想要的部分。但它向我显示了一个错误。它说行太长,无法阅读。
  • it doesn't work 请您以后更具描述性。这个描述对于任何试图帮助你的人来说都是完全没有用的。为什么它不起作用?你有编译错误?运行时错误?出乎意料的输出?你究竟做了什么?你得到了什么?
  • 我说过,我不能摆脱每个空格。在某些索引中,数组中有一些空格。我想我无法消除行之间的空格。

标签: c# parsing file-io multiline


【解决方案1】:

您缺少 StreamReader 的 using 语句,但我不会进行更正,而是将整个文件读取机制替换为;

 string[] lines = File.ReadAllLines("C:\path\to\file.txt");

【讨论】:

    【解决方案2】:

    对于这样的操作,我更喜欢逐行读取文件:

    while (!reader.EndOfStream)
    {
        string line = reader.ReadLine();
    
        // parse values and add to list
    }
    

    您可以检查行的开头是否有开始和结束标记,并在找到结尾时退出循环。

    【讨论】:

      【解决方案3】:

      如果你想去掉每个数组位置的所有空格,你可以像这样擦洗每个索引:

      array[a, b] = Regex.Replace(array2[counter], @"\s", "");

      附带说明,您不需要在其他正则表达式模式中转义空格和破折号。

      【讨论】:

        【解决方案4】:

        这...

        //if the line contains "  "(two spaces), replaces with " "(one space).
        while (lines.Contains("  "))
            lines = Regex.Replace(lines, @"\  ", " ");
        
        string[] items = lines.Split(' ');
        

        ... 可以替换为 ...

        string[] items = lines.Split(' ', StringSplitOptions.RemoveEmptyEntries);
        

        ...这应该可以解决您的空间问题。


        鉴于此,我会以不同的方式进行攻击,并在逐行读取文件时填充对象的属性。这是一个小sn-p(假设您只想要路由大纲):

        var consuming = false;
        var routingOutline = new List<decimal>();
        
        Func<string, bool> isDecimal = text =>
            {
                decimal d;
                return decimal.TryParse(text, out d);
            };
        
        foreach (var line in File.ReadLines("data.txt"))
        {
            if (line.Contains("BOARD_PLACEMENT_OUTLINE"))
                break;
        
            if (line.Contains("BOARD_ROUTING_OUTLINE"))
                consuming = true;
        
            if (consuming)
            {
                var outlineValues = line.Split(' ')
                                        .Where(isDecimal)
                                        .Select(decimal.Parse);
                routingOutline.AddRange(outlineValues);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2014-12-19
          • 2011-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-16
          • 1970-01-01
          相关资源
          最近更新 更多