I am trying to get the number of golfing sessions that have been attended which exists at position position [1] in the text file and add 1 to it. I have manged to do this but now want to update the integer value. Currently the program just writes the new value on the next line instead of updating.
with open("%s.txt" % month, 'r+') as f:
for line in f:
lineList = line.split(",")
if golferssName == lineList[0]:
numSessions = int(lineList[1])
numSessions = int(numSessions) + 1
ineList[1] = numSessions
f.write(str(lineList[1]))
at the moment the text file looks like this:
Tom Jones,1
2
I want the 2 to be where the 1 is :(
this could be another solution:
with open("%s.txt" % month, 'r') as f:
newfilelines = []
filelines = f.readlines()
for fileline in filelines:
lineList = fileline.split(",")
if swimmersName == lineList[0]:
lineList[1] = int(lineList[1]) + 1
newfilelines.append(lineList[0] + ',' + str(lineList[1]) + '\n')
with open("%s.txt" % month, 'w') as f:
for newfileline in newfilelines:
f.write(newfileline)