Toby Toby - 6 months ago 8
Python Question

How to check and change a value in a Text file? (Python 2.7.11)

Text File explained:

Joe,Bloggs,J.bloggs@anemailaddress.com,01269512355, 1 ,0, 0, 0, 0
Fname, Lname, Email, number, Value i want checking ^ , ...,...,...,...


Objective: The check that the number in value[4] of the key is not 0 or 7.
If it is 0 then it changes to 1, and if it is 7 then it changes to 6.
So if it's 0 then + 1, if it's 7 then -1.

Text file:

Joe,Bloggs,J.bloggs@anemailaddress.com,01269512355, 1,0, 0, 0, 0
Sarah,Brown,S.brown@anemailaddress.com,01866522555, 5,0, 0, 0, 0
Andrew,Smith,A.smith@anemailaddress.com,01899512785, 7,0, 0, 0, 0
Kevin,White,K.white@anemailaddress.com,01579122345, 0,0, 0, 0, 0
Samantha,Collins,S.collins@anemailaddress.com,04269916257, 0,0, 0, 0, 0


After the code has run, it should look like this:

Joe,Bloggs,J.bloggs@anemailaddress.com,01269512355, 1,0, 0, 0, 0
Sarah,Brown,S.brown@anemailaddress.com,01866522555, 5,0, 0, 0, 0
Andrew,Smith,A.smith@anemailaddress.com,01899512785, 6,0, 0, 0, 0
Kevin,White,K.white@anemailaddress.com,01579122345, 1,0, 0, 0, 0
Samantha,Collins,S.collins@anemailaddress.com,04269916257, 1,0, 0, 0, 0


The code i have so far produces an error:

fileinfo[j] = i[0] + ',' + i[1] + ',' + i[2] + ',' + i[3] + ',' + str(value) + ',' + i[5] + ',' + i[6] + ',' + i[7]+ ',' + i[8] + '\n'
IndexError: list assignment index out of range


Code:

f = open ("players.txt","r")
fileinfo = f.readlines()
f.close()

j = 0
for i in fileinfo:
i = i.strip()
i = i.split(",")
value = int(i[4])
if value == "0":
value = value + 1
fileinfo[j] = i[0] + ',' + i[1] + ',' + i[2] + ',' + i[3] + ',' + str(value) + ',' + i[5] + ',' + i[6] + ',' + i[7]+ ',' + i[8] + '\n'
j = j + 1

if value == "7":
value = value - 1
fileinfo[j] = i[0] + ',' + i[1] + ',' + i[2] + ',' + i[3] + ',' + str(value) + ',' + i[5] + ',' + i[6] + ',' + i[7]+ ',' + i[8] + '\n'
j = j + 1

f = open ("players.txt","w")
for i in fileinfo:
f.write(i)
f.close()


This is probably a very complex way of doing what i want to do. Please can you help me with my objective. Feel free to rewrite my entire code, but can you explain in detail what you have done. I am quite new to coding.

For future readers

There are two answers that work. I can only tick one of them. Hope this question helps future readers as it has done me.

Answer

The problems with your code are as follows:

  1. You should remove the whitespace around the number before converting it to an int: value = int(i[4]) would crash if there were whitespace around the number. Use value = int(i[4].strip()) to fix this.

  2. You're converting the value to an integer, then comparing that integer to a string. This will always evaluate to False.

    value = int(i[4]) if value == "0":

  3. You're incrementing j twice per loop, which is why your code crashes with an IndexError. I suggest using enumerate instead of manually maintaining j.

The fixed code could look like this:

for j, i in enumerate(fileinfo):
    i = i.strip()
    i = i.split(",")
    value = i[4].strip()
    if value == "0":
        i[4] = "1"
    elif value == "7":
        i[4] = "6"
    fileinfo[j] = ','.join(i) + '\n'