I'm having a bit of trouble searching a file using python regex.
I would like to input a list of regexs and return the lines of the file that match one of them in a jagged list that is indexed in the same way was the rexex list, i.e. if a line matches the 1st regex it will be added with: results[0].append(line)
and if the second is matched: results[1].append(line)
and so on...
import re
def search(path, regex_list):
reg_list = [re.compile(regex) for regex in regex_list]
results = reg_list.__len__()*[[]]
with open(path, 'r') as fp:
for line in fp:
for i, reg in enumerate(reg_list):
if reg.search(line):
results[i].append[line]
return results
print(search("./log", ['1234', '1233']))
I woud like my output to be:
[['log entry 1234\n'], ['log entry 1233\n']]
but what I really get is:
[['log entry 1234\n', 'log entry 1233\n'], ['log entry 1234\n', 'log entry 1233\n']]
I'm pretty new to python so I could be doing something really stupid, any ideas what it is?