1
使用python從用戶輸入文件審查單詞「冬天」 - 「y.txt」寫了一些代碼,但我遇到了錯誤。任何發生的「冬季」一詞都應該消失。幫助和感謝!從文本文件審查單詞並創建新文件
filename = input("Enter file name (without extension): ")
file1 = filename+".txt"
file2 = filename+"_censored.txt"
word = input("Enter the word you are searching for: ")
#In this case, the input would be "winter"
print("\nLooping through the file, line by line.")
in_text_file = open(file1, "r")
out_text_file = open(file2,"w")
for line in in_text_file:
print(line)
out_text_file.write(line)
n = [ ]
def censor(word, filename):
for i in text.split(""):
if i == word:
i = "*" * len(word)
n.append(i)
else:
n.append(i)
return "".join(n)
censor(word,filename)
in_text_file.close()
out_text_file.close()
你得到什麼錯誤? – dartdog
@dartdog剛剛編輯了這個問題! – teresawithoutah
你永遠不會定義「文本」併爲其分配內容 – dartdog