###### tags: `Python`,`TQC` # TQC+ 程式語言Python 905 字串資料刪除 1. 題目說明: 請開啟PYD905.py檔案,依下列題意進行作答,使輸出值符合題意要求。作答完成請另存新檔為PYA905.py再進行評分。 請注意:資料夾或程式碼中所提供的檔案路徑,不可進行變動,data.txt檔案需為UTF-8編碼格式。 2. 設計說明: 請撰寫一程式,要求使用者輸入檔案名稱data.txt和一字串s,顯示該檔案的內容。接著刪除檔案中的字串s,顯示刪除後的檔案內容並存檔。 3. 輸入輸出: 輸入說明 輸入data.txt及一個字串 輸出說明 先輸出原檔案內容,再輸入刪除指定字串後的新檔案內容  :warning:瘋狂做錯的一題,不能在code judger上測試,有時用spyder又看不出差異,相當困擾我的一題~~ ```python= fn = input() de1 = input() with open(fn,"r",encoding="UTF-8") as fp: file1 = fp.read() print("=== Before the deletion") print(file1) file1 = file1.replace(de1,"") print("=== After the deletion") print(file1) with open(fn,'w',encoding='UTF-8') as fp: fp.write(file1) ```
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.