【发布时间】: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