Problem: My code works when value[3] and [4] have 0 in it. However, when i try and add my actual values in, the code messes up (see below).
Text file explained here.
Joe,Bloggs,[email protected],01269512355, 1,15, 0, 0, 0
FName, LName, Email, Number, Division, Points, ..., ...,
Code objective: The code should take the bottom and top two scoring players in each division and -1 or +1 to the division. +1 if low score and -1 if high score. So, if there was 6 people in a division, 2 would go up, 2 would go down and 2 would stay the same division.
Code:
f = open('test copy.txt', 'r')
lines = []
for line in f.readlines():
line = [x.strip() for x in line.split(',')]
line[4] = int(line[4])
line[5] = int(line[5])
lines.append(line)
f.close()
ordered = sorted(zip(range(len(lines)), lines), key=lambda x: x[1][3])
lines[ordered[-1][0]][4] += 1
lines[ordered[-2][0]][4] += 1
lines[ordered[0][0]][4] -= 1
lines[ordered[1][0]][4] -= 1
with open('test copy.txt', 'w') as f:
for line in lines:
line = [str(x) for x in line]
f.write(', '.join(line) + '\n')
Text file that works:
Joe,Bloggs,0,0, 1,15, 0, 0, 0,
Sarah,Brown,0,0, 1,12, 0, 0, 0,
Andrew,Smith,0,0, 1,4, 0, 0, 0,
Ray,Charles,0,0, 1,3, 0, 0, 0,
Kevin,White,0,0, 1,8, 0, 0, 0,
Samantha,Collins,0,0, 1,2, 0, 0, 0,
Test file that needs to work but doesn't:
Joe,Bloggs,[email protected],01269512355, 1,15, 0, 0, 0
Sarah,Brown,[email protected],01866522555, 1,12, 0, 0, 0
Andrew,Smith,[email protected],01899512785, 1,4, 0, 0, 0
Ray,Charles,[email protected],01268712321, 1,3, 0, 0, 0
Kevin,White,[email protected],01579122345, 1,8, 0, 0, 0
Samantha,Collins,[email protected],04269916257, 1,2, 0, 0, 0
desired outcome:
Joe,Bloggs,[email protected],01269512355, 0,15, 0, 0, 0
Sarah,Brown,[email protected],01866522555, 0,12, 0, 0, 0
Andrew,Smith,[email protected],01899512785, 1,4, 0, 0, 0
Ray,Charles,[email protected],01268712321, 2,3, 0, 0, 0
Kevin,White,[email protected],01579122345, 1,8, 0, 0, 0
Samantha,Collins,[email protected],04269916257, 2,2, 0, 0, 0
Because the value[3] is an integer, that seems to be messing with the code in sorting out which number in value[5] is bigger and then changing value[4] to the proper number.
This is what happens to the text file that needs to work:
Joe, Bloggs, [email protected], 01269512355, 0, 15, 0, 0, 0
Sarah, Brown, [email protected], 01866522555, 1, 12, 0, 0, 0
Andrew, Smith, [email protected], 01899512785, 2, 4, 0, 0, 0
Ray, Charles, [email protected], 01268712321, 0, 3, 0, 0, 0
Kevin, White, [email protected], 01579122345, 1, 8, 0, 0, 0
Samantha, Collins, [email protected], 04269916257, 2, 2, 0, 0, 0
As you can see, it is different because of the phone number in value[3].