【问题标题】:Searching a document for specific strings, then print out a part of that string在文档中搜索特定字符串,然后打印出该字符串的一部分
【发布时间】:2021-09-02 17:52:29
【问题描述】:

所以在我的程序中,我试图搜索一个名为 console.log 的文档,其中包含如下行:

65536:KONSOLL:1622118174:NSN:ActivationUnits::HandleActivationUnitsMessages. There is no handler for FNo: 34
65536:KONSOLL:1622177574:NSN:ActivationUnits::HandleActivationUnitsMessages. There is no handler for FNo: 34
65536:KONSOLL:1622190642:NSN:From AutroSafe: 28 5 2021, 08:30:42; 05.046; Service: "Self Verify" mislykket; ;  ; ProcessMsg; F:2177 L:655; 53298;1;13056;; 
65536:KONSOLL:1622204573:NSN:ActivationUnits::HandleActivationUnitsMessages. There is no handler for FNo: 34

在我的输入中,我总是将“自我验证”指定为我在照顾它。我想要输出上的检测器编号(05.046)。但我得到一个错误。

这是我的代码:

import os
import re

pattern = input("What are you searching for? -->")
detectorPattern = re.compile(r'\d\d.\d\d\d')
directory = os.listdir()
for x in range(len(directory)):
    with open(directory[x], 'r') as reader:
        print("Opening " + directory[1])
        # Read and print the entire file line by line
        for line in reader:
            findLine = re.search(pattern, line)
            if findLine is not None:
                mo = detectorPattern.search(findLine)
                print(mo.group())

所以我想做的是去一行,如果我找到“自我验证”,我将在该行中搜索检测器模式中指定的检测器,并将其打印出来。

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\haral\Desktop\PP\SVFinder.py", line 14, in <module>
    mo = detectorPattern.search(findLine)
TypeError: expected string or bytes-like object

【问题讨论】:

  • 那么你的问题是什么?
  • 它不起作用。我收到一个错误,正在更新我的帖子
  • 尝试将mo = detectorPattern.search(findLine) 更改为mo = detectorPattern.search(findLine.string),看看这是否是您想要的。对象 findLine 是一个 re.match 对象,您可能需要字符串。

标签: python python-3.x regex


【解决方案1】:

变化:

mo = detectorPattern.search(findLine)

收件人:

mo = detectorPattern.search(findLine.string)

这将打印:

162219

执行该行时:

print(mo.group())

【讨论】:

    【解决方案2】:

    我建议你直接将line 放入detectorPattern.search。 也请不要把if x is not None:换成if x:

    这个例子应该是这样工作的:

    import re
    
    pattern = "Self Verify"
    reader = ['65536:KONSOLL:1622118174:NSN:ActivationUnits::HandleActivationUnitsMessages. There is no handler for FNo: 34',
              '65536:KONSOLL:1622177574:NSN:ActivationUnits::HandleActivationUnitsMessages. There is no handler for FNo: 34',
              '65536:KONSOLL:1622190642:NSN:From AutroSafe: 28 5 2021, 08:30:42; 05.046; Service: "Self Verify" mislykket; ;  ; ProcessMsg; F:2177 L:655; 53298;1;13056;; ',
              '65536:KONSOLL:1622204573:NSN:ActivationUnits::HandleActivationUnitsMessages. There is no handler for FNo: 34]']
    
    for line in reader:
        findLine = re.search(pattern, line)
        detectorPattern = re.compile(r'\d\d.\d\d\d')
        if findLine:
            detector = detectorPattern.search(line).group()
            print(detector)
    

    输出:

    162219
    

    【讨论】:

      【解决方案3】:

      我想要输出的检测器编号 (05.046)

      这似乎完成了这项工作。我只是在detectorPattern 中按模式更改了findLine。

      import os
      import re
      
      pattern = input("What are you searching for? -->")
      detectorPattern = re.compile(r'\d\d.\d\d\d')
      directory = os.listdir()
      for x in range(len(directory)):
          with open(directory[x], 'r') as reader:
              print("Opening " + directory[1])
              # Read and print the entire file line by line
              for line in reader:
                  findLine = re.search(pattern, line)
                  if findLine is not None:
                      mo = detectorPattern.search(pattern)
                      print(mo.group())
      

      【讨论】:

        猜你喜欢
        • 2012-01-06
        • 2017-03-05
        • 1970-01-01
        • 2021-02-24
        • 1970-01-01
        • 1970-01-01
        • 2015-09-11
        • 2021-12-21
        • 1970-01-01
        相关资源
        最近更新 更多