2

I am not able to add a number to my list that i have in a text file and don't know how to.

Code so far:

def add_player_points():
# Allows the user to add a points onto the players information.

    L = open("players.txt","r+")
    name = raw_input("\n\tPlease enter the name of the player whose points you wish to add: ")
    for line in L:
            s = line.strip()
            string = s.split(",")
            if name == string[0]:
                    opponent = raw_input("\n\t Enter the name of the opponent: ")
                    points = raw_input("\n\t Enter how many points you would like to add?: ")
                    new_points = string[7] + points
    L.close() 

This is a sample of a key in the text file. There are about 100 in the file:

Joe,Bloggs,J.bloggs@anemailaddress.com,01269 512355, 1, 0, 0, 0, 
                                                        ^  

The value that i would like this number to be added to is the 0 besides the number already in there, indicated by an arrow below it. The text file is called players.txt as shown.

A full code answer would be helpful.

Toby
  • 65
  • 7

2 Answers2

0

I didn't like what i wrote earlier, and the use case isn't optimal for fileinput. I took a similar piece of code from the sources and suited it for your needs.

Notice that for each line you modify, you are re-writing an entire file. I strongly suggest changing the way you handle data if performance is a concern.

This code works tho.

from tempfile import mkstemp
from shutil import move
from os import remove, close

def add_player_points():
    file_path = "test.txt"
    name = raw_input("\n\tPlease enter the name of the player whose points you wish to add: ")
    #Create temp file
    fh, abs_path = mkstemp()
    with open(abs_path,'w') as new_file:
        with open(file_path) as old_file:
            for line in old_file:
                stripped_line = line.strip()
                split_string = stripped_line.split(",")
                print name == split_string[0]
                if name == split_string[0]:
                    opponent = raw_input("\n\t Enter the name of the opponent: ")
                    points = raw_input("\n\t Enter how many points you would like to add?: ")
                    temp = int(split_string[5]) + int(points)  # fool proofing the code
                    split_string[5] = str(temp)
                    stripped_line = ','.join(split_string)# line you shove back into the file.  
                    print stripped_line   
                    new_file.write(stripped_line +'\n')
                else:
                    new_file.write(line)
    close(fh)
    #Remove original file
    remove(file_path)
    #Move new file
    move(abs_path, file_path)
  1. Search and replace a line in a file in Python

  2. Editing specific line in text file in python

You wouldn't expect it to be that big of an issue, but it is.

Another tip: might wanna check the module csv - it might be smarter for file editing than what i showed here.

Community
  • 1
  • 1
Olegp
  • 182
  • 2
  • 14
-1

2 issues, first you're never saving your changes to the file. You need to build the string and then save it at the end with L.write("your new string"). Second, you need to cast the points to ints before adding them, change

new_points = string[7] + points

to

new_points = int(string[7]) + int(points)

Edit: Fixed the syntax as mentioned in the comments

CamJohnson26
  • 1,119
  • 1
  • 15
  • 40