TQC+ 程式語言Python 905 字串資料刪除
題目說明:
請撰寫一程式,要求使用者輸入檔案名稱data.txt和一字串s,顯示該檔案的內容。接著刪除檔案中的字串s,顯示刪除後的檔案內容並存檔。
範例輸入1
data.txt
Tomato
範例輸出1
=== Before the deletion
Apple Kiwi Banana
Tomato Pear Durian
=== After the deletion
Apple Kiwi Banana
Pear Durian
範例輸入2
data.txt
Kiwi
範例輸出2
=== Before the deletion
Apple Kiwi Banana
Tomato Pear Durian
=== After the deletion
Apple Banana
Tomato Pear Durian
檔案下載:
data.txt
程式碼:
f_name = input()
string = input()
file = open(f_name, 'r+')
datas = file.read()
print("=== Before the deletion")
print(datas)
print("=== After the deletion")
datas = datas.replace(string, '')
print(datas)
file.seek(0)
file.write(datas)
file.close()
一個評論
自動引用通知: