I currently have a textfile that contains a master list that stores two list like this:
[Gerald D1 Completed D2 Pending]
[Adrian D1 Completed D2 Pending]
How can I edit Adrian's D2 Pending to D2 Completed and store it back into a textfile?
I currently have a textfile that contains a master list that stores two list like this:
[Gerald D1 Completed D2 Pending]
[Adrian D1 Completed D2 Pending]
How can I edit Adrian's D2 Pending to D2 Completed and store it back into a textfile?
Here's a very simplistic solution for this specific case:-
with open('foo.txt', 'r+') as dfile:
lines = dfile.readlines()
dfile.seek(0)
dfile.truncate()
for line in lines:
ls = line.split()
if ls[0] == '[Adrian' and ls[4] != 'Completed]':
ls[4] = 'Completed]\n'
dfile.write(' '.join(ls))
else:
dfile.write(line)