I am trying something pretty simple but i have issue I do not understand. Basically I have a file that is filled with text with this form
Text Accuracy: 0.568221 F1 = 22 recall=0.54
with paramters A=xxx B=11 C=222...
=============================
Text Accuracy: 0.568221 F1 = 22 recall=0.54
with paramters A=xxx B=11 C=222...
=============================
Text Accuracy: 0.568221 F1 = 22 recall=0.54
with paramters A=xxx B=11 C=222...
=============================
Text Accuracy: 0.568221 F1 = 22 recall=0.54
with paramters A=xxx B=11 C=222...
=============================
Text Accuracy: 0.568221 F1 = 22 recall=0.54
with paramters A=xxx B=11 C=222...
=============================
What I want to do is write all blocks of 3 lines that have an accuracy above 0.90 in another file. To navigate through the lines I used the solution proposed here. My code is the following :
with open('G:\Mayeul\Distribution images\Features_importance\LogDecisionTree.txt') as oldfile, open('G:\Mayeul\Distribution images\Features_importance\LogDecisionTree2.txt', 'w') as newfile:
#print(len(oldfile.readlines()))
for line in range(1,int(len(oldfile.readlines()))):
print(line)
if line%3==0:
f=oldfile.readlines()[line-2]
f=f.split(' ')[3]
if int(f)>0.90:
newfile.write(oldfile.readlines()[line-2])
newfile.write(oldfile.readlines()[line-1])
newfile.write(oldfile.readlines()[line])
Starting here I have 2 issues I do not understand ... the first one is
f=oldfile.readlines()[line-2] IndexError: list index out of range
That i don't understand as I print the length that is 13599, and my modulo is working so 3-2=1, no negative number of lines
The second issue that I never got before is that when I uncomment the print(len(oldfile.readlines()))
line, I have no errors as it prints the value, but then stop without doing nothing. It is like the print is killing the program as it does not enter the for loop....
Thx