我無法弄清楚如何將用戶輸入寫入現有文件。該文件已包含一系列字母,稱爲corpus.txt。我想獲取用戶輸入並將其添加到文件中,保存並關閉循環。將用戶輸入寫入文件python
這是我的代碼:
if user_input == "q":
def write_corpus_to_file(mycorpus,myfile):
fd = open(myfile,"w")
input = raw_input("user input")
fd.write(input)
print "Writing corpus to file: ", myfile
print "Goodbye"
break
有什麼建議?
用戶信息的代碼是:
def segment_sequence(corpus, letter1, letter2, letter3):
one_to_two = corpus.count(letter1+letter2)/corpus.count(letter1)
two_to_three = corpus.count(letter2+letter3)/corpus.count(letter2)
print "Here is the proposed word boundary given the training corpus:"
if one_to_two < two_to_three:
print "The proposed end of one word: %r " % target[0]
print "The proposed beginning of the new word: %r" % (target[1] + target[2])
else:
print "The proposed end of one word: %r " % (target[0] + target[1])
print "The proposed beginning of the new word: %r" % target[2]
我也試過這樣:
f = open(myfile, 'w')
mycorpus = ''.join(corpus)
f.write(mycorpus)
f.close()
因爲我要被添加到該文件的用戶輸入,而不是刪除的內容已經有了,但沒有用。
請幫忙!
當你有答案時,你不應該刪除你的問題。問題和答案應該保持在對其他人有用的情況下。可能你可以接受一個正確答案。 – skyking