My code reads through a parsed text file line by line. I am trying to pull a list of fault codes using my reg expression. My problem is that when I include my regex in my if statement, I am not receiving my desired text.
The regex "[:fault]_+[\d|\D]_+[\d|\D]_+[\d|\D]+[Type\S]"
should return a fault code such as :
fault_0A_01_00 Type:Warning, triggered.
Building a list of faults and using this function. I am able to pull my faults. However I did away with this list and decided a regex would be a better option to find the faults rather than having to update my fault list from time to time
def find_fault(self, fault):
count = 0
l_fault = fault
with open(self.file) as f:
for line in f.readlines():
if l_fault in line:
count += 1
self.fault_dict[l_fault] = count
Here is my loop that does not return my desired text based off my regex
def find_fault(self, fault):
count = 0
l_fault = fault
regex_txt = r"[:fault]_+[\d|\D]_+[\d|\D]_+[\d|\D]+[Type\S]"
#pattern = re.compile(regex_txt, re.IGNORECASE)
with open(self.file) as f:
for line in f.readlines():
if l_fault in regex_txt:
count =+ 1
else:
return -1
self.fault_dict[fault] = count
Right now using the regex inside the loop and using the if statement, my dictionary of faults returns as:
{'a': 1, 'e': 1, 'd': 1, 'l': 1, 'p': 1, 'u': 1, 't': 1, 'y': 1, '_': 1, 'D': 1}
My expectations are something such as:
{':fault_01_00_00': 1, ':fault_01_00_01': 1, ':fault_01_00_02': 1, ':fault_01_00_03': 1, ':fault_01_00_04': 1, ':fault_01_00_05': 1, ':fault_01_00_06': 1, ':fault_01_00_07': 1, ':fault_01_00_08': 1, ':fault_01_00_09': 1}