i have a file.txt
that look like this.
testings 1
response 1-a
time 32s
testings 2
response 2-a
time 32s
testings 3
*blank*
testings 4
error
testings 5
response 5-a
time 26s
and prints
['testings 1', 'testings 2', 'testings 3', 'testings 4', 'testings 5']
['response 1-a', 'response 2-a', 'response 5-a']
['time 32s', 'time 20s', 'time 26s']
So it´s a simpel code i have, it opens the file, uses readlines()
and looks for the keywords testings
,response
and time
then appends the string to 3 seperat lists. As shown in the file.txt
some testings x
are either *blank*
or has an error
instead off a response
. My problem is that i need the lists to always have the same lenght. Like this:
['testings 1', 'testings 2', 'testings 3', 'testings 4', 'testings 5']
['response 1-a', 'response 2-a', '*error*', '*error*', 'response 5-a']
['time 32s', 'time 20s', '*error*', '*error*', 'time 26s']
So i was thinking if it´s posbile to "read for 3 lines at the same time" and have a if-statment where all the 3 lines need to have the right keywords ("be True") or else insert *error*
in the response and time list to keep the lenght right. Or is there even a better way to keep 3 list at the same lenght?
test = []
response = []
time =[]
with open("textfile.txt",'r') as txt_file:
for line in txt_file.readlines():
if ("testings") in line:
test.append(line.strip())
if ("response") in line:
response.append(line.strip())
if ("time") in line:
time.append(line.strip())
print (response)
print (test)
print (time)