說明:
請撰寫一程式,要求使用者輸入檔名data.txt、字串s1和字串s2。程式將檔案中的字串s1以s2取代之。
檔案連結:data.txt (請另存檔案,必須與程式同一資料夾)
範例輸入:
data.txt
pen
sneakers
範例輸出:=== Before the replacement
watch shoes skirt
pen trunks pants
=== After the replacement
watch shoes skirt
sneakers trunks pants
程式碼:
fn, str1, str2 = input(), input(), input()
with open(fn,'r',encoding='UTF-8') as fp:
fpStr = fp.read()
print('=== Before the replacement')
print( fpStr )
fpStr = fpStr.replace( str1, str2 )
print('=== After the replacement')
print( fpStr )
沒有留言:
張貼留言