下面是我的代码看起来像至今:如何在python中写入和读取文件?
restart = 'y'
while (True):
sentence = input("What is your sentence?: ")
sentence_split = sentence.split()
sentence2 = [0]
print(sentence)
for count, i in enumerate(sentence_split):
if sentence_split.count(i) < 2:
sentence2.append(max(sentence2) + 1)
else:
sentence2.append(sentence_split.index(i) +1)
sentence2.remove(0)
print(sentence2)
outfile = open("testing.txt", "wt")
outfile.write(sentence)
outfile.close()
print (outfile)
restart = input("would you like restart the programme y/n?").lower()
if (restart == "n"):
print ("programme terminated")
break
elif (restart == "y"):
pass
else:
print ("Please enter y or n")
我需要知道做什么,这样我的程序打开文件,节省了句输入,而且重建了一句,然后将数字能够打印文件。 (即时猜测这是阅读部分)。正如你可能知道的那样,我对文件的阅读和写作一无所知,所以请写下你的答案,这样小白就能理解。此外,与文件相关的代码的一部分是在黑暗中的完整刺伤,并从不同的网站采取,所以不要认为我有这方面的知识。
不要从随机的网站,使用[官方教程](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) –
无论如何,你可能想要在while循环之前打开文件,然后关闭它们。 –