Converts an integer in a list into a string to write to a text file and later retrieved

advertisements

I am trying to make a stats.txt file where I can store wins, draws, and losses. Thus far, the only way I have been able to retrieve the values AND modify them when the program runs is by opening them with .readlines() and then assigning them to a variable as an integer within a list (see below).

My issue is that I can't seem to find a way to write them back to a text file as they are in list form. Can anyone point me in the right direction?

The text file is in this format:

0\n
0\n
0\n

Nothing else is written in it other that the three 0's, followed by enter.

My code is as follows:

stats = open("C:/Users/JawshBawx/Documents/LiClipse Workspace/RPScLSp/RPScLSp_Src/Stats.txt","r")
lines = stats.readlines()
wins = list(int(val) for val in lines[0].split())
draws = list(int(val) for val in lines[1].split())
losses = list(int(val) for val in lines[2].split())

If I assign these without list, output becomes: generator object <genexpr> at 0x0000011CEF216CA8 when printed, and I can't modify the values with +1.

if result == 0:
    print("Draw!")
    draws = [x + 1 for x in draws]

elif result == 1:
    print(ai_var+" Wins!")
    losses = [x + 1 for x in losses]

elif result == 2:
    print(ai_var+" Wins!")
    losses = [x + 1 for x in losses]

elif result == 3:
    print(user_var+" Wins!")
    wins = [x + 1 for x in wins]

else:
    print(user_var+" Wins!")
    wins = [x + 1 for x in wins]

print(wins)
print(draws)
print(losses)

stats.close
with open("C:/Users/JawshBawx/Documents/LiClipse Workspace/RPScLSp/RPScLSp_Src/Stats.txt","w") as f:
    f.write(wins)
    f.write(draws)
    f.write(losses)

My result is:

TypeError: write() argument must be str, not list

I cannot get this to convert to string, and write in in such a way that it will be retrieved in the same format as I need it to be as to be able to process i every time the program runs.

I'd rather just import the values in the text file as integers, modify them, then write them as integers, but I haven't found a way to do that yet. I know there must be better ways to do the above.


  1. You should use str.strip() instead of str.split() in this case. str.split() split the string by empty letters (spaces, newlines, tabs, etc.) and return a list.

    However, it looks like that you don't need a list here and just want remove that \n, then, you can use str.strip(). Which removes the empty letters after and before the string.

    Demo:

    >>> s = '0\n'
    >>> s
    '0\n'
    >>> s.strip()
    '0'
    >>> s.split()
    ['0']
    >>> int(s.strip())
    0
    
    
  2. Then, it's an int object. So you can just totally drop these list comprehension and use object += 1.

    However, you can just use str() function to convert it to a string object when you write it back to a file.

    Remember also add a +\n if you don't want them all in one line:

    with open("C:/Users/JawshBawx/Documents/LiClipse Workspace/RPScLSp/RPScLSp_Src/Stats.txt", "r") as f:
        lines = f.readlines()
        wins = int(lines[0].strip())
        draws = int(lines[1].strip())
        losses = int(lines[2].strip())
    
    if result == 0:
        print("Draw!")
        draws += 1
    
    elif result in [1, 2]:
        print(ai_var+" Wins!")
        losses += 1
    
    else:
        print(user_var+" Wins!")
        wins += 1
    
    with open("C:/Users/JawshBawx/Documents/LiClipse Workspace/RPScLSp/RPScLSp_Src/Stats.txt","w") as f:
        f.write(str(wins)+'\n')
        f.write(str(draws)+'\n')
        f.write(str(losses)+'\n')