2017-11-25 172 views
1

我不是很有經驗,所以請儘快知道我盡力而爲。如何將文件的第一個內容(例如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

回答

1

「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

0

假設我理解你的問題,這應該做到這一點。它將打開文件並在將值附加到用戶輸入之前獲取值並將它們寫入文件。我已經評論過它,如果你迷路了,我希望這會有所幫助。

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的

0

現在,它的工作原理

您必須使用「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

相關問題