блок 1:
АААААА
.... (то, что идет после ААААААА и перед следующим ААААААА)
Номера строк из текстового файла, где это тоже находится: ...
блок 2:
АААААА
.... (то, что идет после ААААААА и перед следующим ААААААА)
Номера строк из текстового файла, где это тоже встречается: ...
И так on\
Я попробовал сделать так, чтобы сначала вводил в массив номера строк вхождений "AAAAAA". Потом я перечитывал эти блоки из файла и смотрел, есть ли они в словаре, если нет, то добавлял их в словарь, и если они были, то записывал номер строки из текстового файла. Но он работает очень медленно и выдал мне ошибку памяти. Я проверил свой алгоритм на 10 блоках, он работает корректно.\
Код: Выделить всё
from itertools import islice
mas_inst = [] #An array that stores the indexes of the lines "AAAAAA" from the text file
block_nach = []
count = 0
dict = {}
with open("C:/Users/Azerty/Downloads/MyHypervisorDriver.vmp/Copuies.tag", mode='r') as file:
for line in islice(file, 85, None): #We go through the test file from line 85 (blocks start only from this line) and to the end of the file
if line.strip() == 'AAAAAA': #If a line equal to "AAAAAAA" is encountered, then the number of this line is entered into the array
count = count + 1
mas_inst.append(count)
else:
count = count + 1
file.seek(0) #We go to the beginning of the file
for i in range(0, len(mas_inst)-1): #We take the beginning and end of the block from the mas_inst array and enter all the lines of the block into the array
for line1 in islice(file, (84 + mas_inst[i]), (mas_inst[i+1] + 84)):
block_nach.append(line1)
block_nach = tuple(block_nach)
if block_nach not in dict: #We look to see if this block is in the dictionary or not
dict[block_nach] = [85+mas_inst[i]]
else:
dict[block_nach] = dict[block_nach] + [85+mas_inst[i]]
block_nach = list(block_nach)
block_nach.clear()
file.seek(1, 0)
with open("C:/Users/Azerty/Downloads/MyHypervisorDriver.vmp/Result.txt", 'w') as file_result: #We write blocks from the dictionary into a new text file
for item in dict:
file_result.writelines(item)
file_result.write('Строчки в которых встречается блок')
file_result.write(str(dict[item]))
file_result.write('\n')
Подробнее здесь: https://stackoverflow.com/questions/793 ... -text-file