Python 如何将用户输入的号码添加到文本文件列表中?

Python 如何将用户输入的号码添加到文本文件列表中?,python,Python,我无法将文本文件中的数字添加到我的列表中,并且不知道如何添加 迄今为止的代码: 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

我无法将文本文件中的数字添加到我的列表中,并且不知道如何添加

迄今为止的代码:

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() 
这是文本文件中的密钥示例。文件中大约有100个:

Joe,Bloggs,J.bloggs@anemailaddress.com,01269 512355, 1, 0, 0, 0, 
                                                        ^  
我希望将此数字添加到的值是0,而不是已经在其中的数字,由其下方的箭头指示。文本文件名为players.txt,如图所示


完整的代码答案会很有帮助。

2个问题,首先,您永远不会将更改保存到文件中。您需要构建字符串,然后在最后用L.writeyour新字符串保存它。第二,您需要在添加点之前将点强制转换为整数,然后再进行更改

new_points = string[7] + points


编辑:修复了注释中提到的语法问题,首先,您永远不会保存对文件的更改。您需要构建字符串,然后在最后用L.writeyour新字符串保存它。第二,您需要在添加点之前将点强制转换为整数,然后再进行更改

new_points = string[7] + points


编辑:修复了注释中提到的语法

我不喜欢我之前写的内容,并且用例对于fileinput不是最佳的。我从源代码中获取了一段类似的代码,并使其适合您的需要

请注意,对于修改的每一行,您都在重新编写整个文件。我强烈建议,如果性能是一个问题,请改变处理数据的方式

这段代码对你有用

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)
你不会认为这是个大问题,但事实确实如此


另一个提示:可能需要检查模块csv-它可能比我在这里展示的更适合文件编辑。

我不喜欢我之前写的内容,而且用例对于文件输入来说也不是最优的。我从源代码中获取了一段类似的代码,并使其适合您的需要

请注意,对于修改的每一行,您都在重新编写整个文件。我强烈建议,如果性能是一个问题,请改变处理数据的方式

这段代码对你有用

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)
你不会认为这是个大问题,但事实确实如此


另一个提示:可能想检查模块csv-它可能比我在这里展示的更适合文件编辑。

我建议再看一看函数调用的语法。我建议再看一看函数调用的语法。我发现这段代码删除了我的文本文件。我试图摆弄它,但没有用:非常感谢你的帮助:非常感谢我发现这段代码删除了我的文本文件。我试着摆弄它,但没有用:非常感谢你的帮助:非常感谢