如何将文件内容添加到python中的变量3.5

如何将文件内容添加到python中的变量3.5

问题描述:

我不是很有经验,所以请尽快知道我尽力而为。如何将文件的第一个内容(例如65)添加到新输入的号码,然后覆盖该文件以保存它?如何将文件内容添加到python中的变量3.5

任何意见非常感谢。 这里是我的编程:

henry = 0 
emily = 0 
george = 0 
points = 0 

file = open('nfc.txt','r+') 
for line in file: 
    print(line) 

if input("Who would you like to give points to? ") == "henry": 
    points = int(input("How many points would you like to give to Henry? ")) 
    henry = henry + points 
    print("Henry now has",henry, "points") 
    points = 0 
    file.write(str(henry)+" ") 
    file.write(str(emily)+" ") 
    file.write(str(george)+" ") 
    file.close() 

else: 
    print("That name is not valid") 
+0

我相信你正在打开文件只读。看看打开的文档。另外看看pdb并试验一下调试。我会放一些照片来看看发生了什么。 – Lewis909

+0

究竟是什么不工作,当我复制你的代码我可以得到nfc.txt显示'2 0 0'如果我的输入给亨利和2。然而你的代码将只为'henry'做一些事情,所以它赢了不适用于艾米莉或乔治,但我相信你可以自己弄清楚为什么自己已经走到了这一步。快乐的bug狩猎。 – ahed87

+0

你想改变文件中的值吗?请显示'nfc.txt'的内容,然后显示您期望改变的内容。 – cdarke

“nfc.txt”当你的代码工作中存在的目录。如果该文件不存在,则使用'w +'。请注意,如果文件已经存在,那么它会覆盖现有文件。以下是更多信息的链接:https://www.tutorialspoint.com/python3/python_files_io.htm。另外,请考虑ahed87发表的评论。 我希望它会有所帮助。 p.s:新的编辑,以改善答案

+0

不是,'+'允许写入文件。 – ahed87

+0

所以在w +中打开文件而不是写入,它会覆盖以前的nfc.txt文件的内容? @无牙 – Gurneyguy

+0

是的,从我提供的链接引用:w +打开一个文件,用于写入和读取。如果文件存在,则覆盖现有文件。如果该文件不存在,则创建一个用于读取和写入的新文件。 – Toothless

假设我理解你的问题,这应该做到这一点。它将打开文件并在将值附加到用户输入之前获取值并将它们写入文件。我已经评论过它,如果你迷路了,我希望这会有所帮助。

file = open('nfc.txt', 'r+') ### opens file 
f = file.read() ### reads file and assigns it to variable f 
f = f.splitlines() ### slits file into list at any neline "\n" 
scores = {} ### creates dictionary to store data 


for line in f: 
    line = line.replace(':', '') ### replaces colons with nothing 
    line = line.split() ### splits name from score 
    scores[line[0]] = int(line[1]) ###appends dictionary so name is key and score is values 

name = input("Who would you like to give points to?").lower() ### user input 
if name in scores.keys(): ### checks to see if input is a dict key 
    point = int(input(
      "How many points would you like to give to {}?".format(name))) ### formats name into next input question 
    scores[name] += point ### adds value to current score 
    scores['total'] += point ### adds value to change total 

    file.seek(0) ### sets position in file to start 
    file.truncate() ### deletes all data in current file 
    for key in list(scores.keys()): ### gets all keys from dict to ittereate 
     file.write("{}: {}\n".format(key, str(scores[key]))) ### writes info to file with \n for new person 
    file.close() ### closes file IMPORTANT 

else: 
    print("That name is not valid") 

我希望你不介意滚动因为我知道它的评论是不是很Python的

现在,它的工作原理

您必须使用“W”中写文件

henry = 0 
emily = 0 
george = 0 
points = 0 

file = open('nfc.txt', 'w+') 
for line in file: 
    print(line) 

if input("Who would you like to give points to? ") == "henry": 
    points = int(input("How many points would you like to give to Henry? ")) 
    henry = henry + points 
    print("Henry now has", henry, "points") 
    points = 0 
    file.write(str(henry) + " ") 
    file.write(str(emily) + " ") 
    file.write(str(george) + " ") 
    file.close() 

else: 
    print("That name is not valid") 

enter image description here

,并在文件中你得到了这个

enter image description here