1

我一直在手动搜索运行程序产生的大文件。我已经成功地提取了一些信息块,但我一直试图提取最后三个块。块的结构如下:

尝试了几个 re 表达式但没有成功,例如:

v2 = re.findall(r'(?s)\(VFSCAN\) AT TIME =(.*?)100 BUSES WITH LOW VOLTAGE DEVIATION BELOW.*?\s*$',wholefile)

Wholefile 是我读入的整个文件。该文件有以下几个部分,我想将它们全部提取出来,以便找到最后出现的条目,例如 ( 18436 [LENZIE 618.0] -0.245)。然后,我将用时间解析该行以确定何时发生。我必须对“电压偏差”“电压”和“频率”做同样的事情。如果我发现如何匹配一个可变长度,多行部分,其他部分应该相同。我的问题是知道何时结束搜索。我使用的事实是搜索应该在最后一个空白行结束(因此我使用\s*$)。例如,我正在使用 findall 来提取所有这些部分的电压偏差。

我对 python 中模式的 VERBOSE 定义也有疑问。我似乎没有工作(下)。难道我做错了什么?

(VFSCAN) AT TIME =  1.1800 UP TO  100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:

X ----- BUS ------ X    VDEV       X ----- BUS ------ X    VDEV
18436 [LENZIE 618.0]   -0.245      18433 [LENZIE 318.0]   -0.245     
18431 [LENZIE 118.0]   -0.214      18432 [LENZIE 218.0]   -0.214     
18435 [LENZIE 518.0]   -0.214      18434 [LENZIE 418.0]   -0.214     

(VFSCAN) AT TIME =  2.6267 UP TO  100 BUSES WITH LOW VOLTAGE BELOW  0.700:

X ----- BUS ------ X    VOLT       X ----- BUS ------ X    VOLT
65191 [BONANZA 24.0]    0.439      65194 [CHAPITA  138]    0.581     
65192 [BONANZA  138]    0.585      65371 [COVE TP  138]    0.694     
66278 [RANGELY  138]    0.698     

(VFSCAN) AT TIME =  6.0632 UP TO  100 BUSES WITH LOW FREQUENCY BELOW 59.600:

X ----- BUS ------ X    FREQ       X ----- BUS ------ X    FREQ
27117 [WTGCP   .600]   59.443      27123 [WTGGE2  .570]   59.490     
27119 [WTGGE   .570]   59.492      26040 [INTERM2G26.0]   59.492     
26039 [INTERM1G26.0]   59.492     

pattern = r"""
(?s)                                                            # Tell Regex to span multiple lines
\(VFSCAN\).*100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:  # Literal string to serach for
(\s*$).*?                                                        # This search for an empty line
X ----- BUS ------ X    VOLT       X ----- BUS ------ X    VOLT   # Literal string to search            (\d{5}.*).*?                                                         # Multiple lines starting with numbers
\s*$                                                                 # This search ends with an empty line
"""
regex = re.compile(pattern, re.VERBOSE)

第二天 在尝试了几个小时后,我想出了以下内容。第一个匹配所有内容(不是我需要的),第二个我确定可以使用的文件与我的测试文件不匹配。

第一的:

(?s)^\(VFSCAN\).*100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:.*(\s*$)?

第二:

(?m)(?s)^\(VFSCAN\).*100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:^\s*$^X ----- BUS ------ X    VDEV.*?
(.*?)
^\s*$

使用这些正则表达式,我试图完全匹配文件的以下部分。

(VFSCAN) AT TIME =  1.1800 UP TO  100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:

X ----- BUS ------ X    VDEV       X ----- BUS ------ X    VDEV
18436 [LENZIE 618.0]   -0.245      18433 [LENZIE 318.0]   -0.245     
18431 [LENZIE 118.0]   -0.214      18432 [LENZIE 218.0]   -0.214     
18435 [LENZIE 518.0]   -0.214      18434 [LENZIE 418.0]   -0.214     

我需要一些帮助来修复模式,以便我可以选择上述模式。

我对以下文字有疑问。我只想提取所有方括号“[]”中的时间和相关项目。

test3 = r'''(VFSCAN) AT TIME =  1.1800 UP TO  100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -    0.200:

X ----- BUS ------ X    VDEV       X ----- BUS ------ X    VDEV
18436 [LENZIE 618.0]   -0.245      18433 [LENZIE 318.0]   -0.245     
18431 [LENZIE 118.0]   -0.214      18435 [LENZIE 518.0]   -0.214     
18434 [LENZIE 418.0]   -0.214      18432 [LENZIE 218.0]   -0.214     

(VFSCAN) AT TIME =  1.5167 UP TO  100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:

X ----- BUS ------ X    VDEV       X ----- BUS ------ X    VDEV
69036 [DNLP2G21.575]   -0.414      69038 [DNLP2G22.575]   -0.414     
69040 [DNLP2G23.575]   -0.414      69032 [DNLP1_G1.575]   -0.402     
65460 [DIFICULT 230]   -0.384      69027 [7MIHL G1.575]   -0.355     
69076 [HORIZ_G .575]   -0.303      67237 [MEDBOWCO 115]   -0.301     
67940 [STNDPSVC 230]   -0.300      65976 [MINERS  34.5]   -0.294     
65585 [FT CRK1 34.5]   -0.261      65584 [FT CRK2 34.5]   -0.261     
69073 [HIPLN_G .575]   -0.214     

(VFSCAN) AT TIME =  1.1800 UP TO  100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:

X ----- BUS ------ X    VDEV       X ----- BUS ------ X    VDEV
65191 [BONANZA 24.0]   -0.572      65192 [BONANZA  138]   -0.434     
65194 [CHAPITA  138]   -0.433      66278 [RANGELY  138]   -0.320     
65371 [COVE TP  138]   -0.302      79265 [CALAMRDG 138]   -0.286     
79400 [DES.MINE 138]   -0.285      65086 [ASHLEY  69.0]   -0.284     
79067 [VERNAL   138]   -0.277      67257 [MOONLAK269.0]   -0.268     
67256 [MOONLAK169.0]   -0.266      79264 [W.RV.CTY 138]   -0.206     

'''

当我将 findall 与我得到的模式一起使用时。

[('1.1800', 'DEVIATION', 'LENZIE 218.0'), ('1.5167', 'DEVIATION', 'HIPLN_G .575'), ('1.1800', 'DEVIATION', 'W.RV.CTY 138')]

我的列表中应该有 30 多个匹配的元组。

4

2 回答 2

2

正则表达式提取字段

\(VFSCAN\)[^=]*=\s*    # first line of a section: (VFSCAN) AT TIME =  1.1800 UP TO  100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200
(\d*(?:\.\d+)?)        # group 1 - first number of first line: 1.1800
\D+
(\d+)                  # group 2 - second number of first line: 100
[^\d-]+
(-?\d*(?:\.\d+)?)      # group 3 - last number of first line: -0.200
\D+                    # skip second line
(?:                    # a data line: 18436 [LENZIE 618.0] -0.245 18433 [LENZIE 318.0] -0.245
  (?:                  # a data entry: 18436 [LENZIE 618.0] -0.245
    (\d+)              # group 4 - first number in an entry: 18436
    \s+\[
    (.*?)              # group 5 - words in brackets: LENZIE
    (-?\d*(?:\.\d+)?)  # group 6 - number in brackets: 618.0
    \]\s*
    (\S*)              # group 7 - last number (VDEV): -0.245
    \s*
  )+
  (?=[\r\n\s]+|$)
)+

BUSES WITH LOW VOLTAGE DEVIATION BELOW介于第 2 组和第 3 组之间 ( [^\d-]+)。因此,您可以执行以下操作之一:

选项1

您也可以捕获此部分以稍后检查它是否是您想要的部分。只需在其周围添加括号,使其成为第 3 个捕获组:

[^\d-]+=> ([^\d-]+)

选项 2

或者您可以更改正则表达式的相同部分以仅与所需部分匹配。在这种情况下,正则表达式只匹配指定的部分而不是每个部分:

[^\d-]+=>\s+BUSES\s+WITH\s+LOW\s+VOLTAGE\s+DEVIATION\s+BELOW\s+

如果要匹配这两行:

BUSES WITH LOW VOLTAGE DEVIATION BELOW
BUSES WITH LOW FREQUENCY BELOW

然后,您可以使用替代 ( |) 语法编写更改部分((?:...)表示不捕获该组):

[^\d-]+=>\s+BUSES\s+WITH\s+LOW\s+(?:VOLTAGE\s+DEVIATION|FREQUENCY)\s+BELOW\s+

性能改进

捕获组

可以删除不需要的数组捕获组,例如(xyz)=> xyz,或者以这种方式使其不捕获:(xyz)=>(?:xyz)

不必要的选择

更改.*.+可能会导致一些性能提升。

改进的正则表达式

下面的正则表达式是上述正则表达式的改进版本:

\(VFSCAN\)[^=]*=\s*    # first line of a section: (VFSCAN) AT TIME =  1.1800 UP TO  100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200
(\d*(?:\.\d+)?)        # group 1 - first number of first line: 1.1800
\D+
\d+                    # second number of first line: 100
[^\d-]+
-?\d*(?:\.\d+)?        # last number of first line: -0.200
\D+                    # skip second line
(?:                    # a data line: 18436 [LENZIE 618.0] -0.245 18433 [LENZIE 318.0] -0.245
  (?:                  # a data entry: 18436 [LENZIE 618.0] -0.245
    \d+                # first number in an entry: 18436
    \s+\[
    (.+?)              # group 2 - words in brackets: LENZIE
    -?\d*(?:\.\d+)?    # number in brackets: 618.0
    \]\s+
    \S+                # last number (VDEV): -0.245
    \s*
  )+
  (?=[\r\n\s]+|$)
)+
于 2012-09-03T20:30:11.210 回答
0

VOLT你正在尝试匹配VDEV

(VFSCAN) AT TIME =  1.1800 UP TO  100 BUSES WITH LOW VOLTAGE DEVIATION BELOW -0.200:

X ----- BUS ------ X    VDEV       X ----- BUS ------ X    VDEV

-0.200或者您正在尝试匹配0.700

(VFSCAN) AT TIME =  2.6267 UP TO  100 BUSES WITH LOW VOLTAGE BELOW  0.700:

X ----- BUS ------ X    VOLT       X ----- BUS ------ X    VOLT
于 2012-09-03T05:36:48.380 回答