读取文本文件的方法

时间:2016-11-30 18:04:28

标签: c# wpf text-files

我有预定义的List<string> Wellnames = new List<string>(){"CAM1","CAM2","CAM3"};

我有一个文本文件,它的格式与显示的格式完全相同。如何提取/读取示例中突出显示的数字? I,E。用于CAM1,类似用于CAM2和CAM3。

这里是文件的摘录:

1------------------------------------------------------------------------------------------------------------------------------913 
 ALLOC                  Allocation/bundle report                               10835.0000 Days  report step 228,  1 Sep 2015
 ------------------------------------------------------------------------------------------------------------------------------
 |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
 |        Streamline      |         |                                                                       |                                   |                       |           |           |
 |          Bundle        |         |                              Surface Rate                             |          Surface Volumes          |                       |           |           |
 |------------------------|  Flow   |-----------------------------------------------------------------------------------------------------------|   Total Reservoir     |   Pore    |PV weighted|
 |  Start     |  End      | Direct  |          Oil          |         Water         |          Gas          |   Oil     |   Water   |   Gas     |       Rate            |  Volume   |  Pressure |
 |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
 |                                  |   SM3/D   | Fraction  |   SM3/D   | Fraction  |   SM3/D   | Fraction  |    SM3    |    SM3    |  SM3      |  RM3/D    | Fraction  |    RM3    |   Bar     |
 |                                  |-----------------------------------------------------------------------------------------------------------------------------------------------------------|
 | CAM1                   |         | 9.13e-001 |           | 3.14e+001 |           | 9.13e-001 |           | 7.55e+004 | 1.50e+005 | 7.55e+004 | 3.26e+001 | 1.06e+000 | 2.29e+005 | 6.21e+001 | 
 | CAM1        CAM138     | Outflow | 2.82e-001 | 3.09e-001 | 9.63e+000 | 3.07e-001 | 2.82e-001 | 3.09e-001 | 1.99e+004 | 3.88e+004 | 1.99e+004 | 1.00e+001 | 3.07e-001 | 5.96e+004 | 6.17e+001 | 
 | CAM1        CAM255     | Outflow | 3.34e-002 | 3.66e-002 | 3.00e+000 | 9.57e-002 | 3.34e-002 | 3.66e-002 | 1.86e+004 | 4.05e+004 | 1.86e+004 | 3.07e+000 | 9.40e-002 | 6.00e+004 | 6.30e+001 | 
 | CAM1        CAM177     | Outflow | 2.12e-001 | 2.32e-001 | 1.10e+001 | 3.50e-001 | 2.12e-001 | 2.32e-001 | 1.94e+004 | 3.66e+004 | 1.94e+004 | 1.13e+001 | 3.46e-001 | 5.68e+004 | 6.13e+001 | 
 | CAM1        CAM582     | Outflow | 3.17e-001 | 3.47e-001 | 5.72e+000 | 1.82e-001 | 3.17e-001 | 3.47e-001 | 7.77e+003 | 1.33e+004 | 7.77e+003 | 6.10e+000 | 1.87e-001 | 2.14e+004 | 6.13e+001 | 
 | CAM1        CAM354     | Outflow | 6.87e-002 | 7.53e-002 | 2.05e+000 | 6.53e-002 | 6.87e-002 | 7.53e-002 | 9.80e+003 | 2.04e+004 | 9.80e+003 | 2.14e+000 | 6.56e-002 | 3.07e+004 | 6.34e+001 | 
 |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

enter image description here

3 个答案:

答案 0 :(得分:1)

我假设您知道如何逐行阅读文件。在字符串中有特定数据行后,可以在&#39; |&#39;上使用Split()方法。字符。您将返回一个从零开始的数组,每个元素保存一列中的数据。索引到索引0处的数组并修剪该值以检查您的标识符CAM1等。如果它是您想要的那个,您可以从数组中的索引中获取其他值。

对于严格的数据格式,似乎是最简单的方法。

答案 1 :(得分:1)

IMO解决此问题的最佳方法是使用StreamReader逐行迭代文件,然后解析出必要的信息。

public dynamic Read(string file)
{
    using (var streamReader = new StreamReader(File.OpenRead(file))
    {
        while ((var line = streamReader.ReadLine()) != null)
        {
            if (line.StartsWith("| CAM", StringComparison.OrdinalIgnoreCase)) 
            {
                var columns = line.Split('|').Select(x => x.Trim());

                // Parse the values here, put them in a DTO
            }
        }
    }
}

答案 2 :(得分:1)

这个正则表达式可以满足您的需求。

@"\|\s+CAM1\s+(\S+)\s+\|\s+\S+\s+\|\s+(\S+)\s+\|\s+\S+\s+\|\s+(\S+)\s+\|\s+\S+\s+\|\s+(\S+)"

只需更改CAM1即可找到您想要的内容。

我为你准备了一个例子here