Здесь следует отметить пару вещей.
1) Забота о пустых строках в файле .txt
.
2) Итерации для получения связанного результата, например TC1 Pass
.
3) Добавление / вставка связанной пары в сетку назад.
Подход:
Создайте список, содержащий все данные из файла .txt
, а затем выполните итерацию для получения парного результата, который впоследствии можно будет вставить в сетку.
logFile = "list.txt"
with open(logFile) as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
# flag
nextLine = False
# list to save the lines
textList = []
for line in content:
find_TC = line.find('TC')
if find_TC > 0:
nextLine = not nextLine
else:
if nextLine:
pass
else:
textList.append(line)
print('\n')
print('Text list ..')
print(textList)
j = 0
for i in range(j, len(textList)):
if j < len(textList):
print(textList[j], textList[j + 1]) # Insert into the gird here instead of print
j = j + 2
ВЫВОД:
Текстовый список ..
['TC1', 'Pass', 'TC2', 'Fail', 'TC3', 'Pass']
TC1 Pass
TC2 Fail
TC3 Pass
EDIT:
После новых изменений OP в текстовом файле
j = 0
for i in range(j, len(textList)):
if j < len(textList):
print(textList[j], textList[j + 1], textList[j+2]) # Insert into the gird here instead of print
j = j + 3