The following code causes a mysterious error with which I cannot find a solution. It works great when I tested it in a larger module, so I can’t understand why this is not working:
code
import csv with open('studentinfo.txt','a') as fo:
Error message
sorted_list.sort(key=lambda x: x[2])
It is worth noting that the code works fine when there are no additions to the contents of the file. When adding a student to the file, the CORT does not work.
The original contents of the file
001,Joe,Bloggs,Test1:99,Test2:100,Test3:1 002,Ash,Smith,Test1:20,Test2:20,Test3:100 003,Jonathan,Peter,Test1:99,Test2:33,Test3:44
The contents of the file when adding the student being tested:
001,Joe,Bloggs,Test1:99,Test2:100,Test3:1 002,Ash,Smith,Test1:20,Test2:20,Test3:100 003,Jonathan,Peter,Test1:99,Test2:33,Test3:44 006,Mulch,Cart,Test1:99,Test2:22,Test3:11
The resulting error occurs at this stage (when a new student is added). The sort function otherwise works fine.
Update and clarification:
For training purposes, I need it to work as repl.it AND IDLE>
If someone can send repl.it as an answer (with my code above, working), which also works when it is implemented in IDLE with a txt file, I will accept as an answer.