![TQC+ 程式語言Python 905 字串資料刪除 TQC+ 程式語言Python 905 字串資料刪除](https://i0.wp.com/jbprogramnotes.com/wp-content/uploads/2020/05/TQC-252B-2BPython.png?resize=1024%2C576&ssl=1)
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()
TQC+ 程式語言Python 第9類:檔案與異常處理
如果覺得文章內容還不錯的話,麻煩請幫我點個讚!感謝
可以多點幾次喔~~
第一次點讚需使用 Google 或 Facebook 帳號註冊
![TQC+ 程式語言Python 906 字串資料取代 TQC+ 程式語言Python 906 字串資料取代](https://i0.wp.com/jbprogramnotes.com/wp-content/uploads/2020/05/TQC-252B-2BPython.png?resize=75%2C75&ssl=1)
![TQC+ 程式語言Python 904 資料計算 TQC+ 程式語言Python 904 資料計算](https://i0.wp.com/jbprogramnotes.com/wp-content/uploads/2020/05/TQC-252B-2BPython.png?resize=75%2C75&ssl=1)
一個評論
自動引用通知: