[問題] replace multiple string in text file

看板Python作者 (demon)時間13年前 (2012/11/15 15:32), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
我想讀取某個txt檔,然後取代裡面的字元。 例如~ I -> You , is -> are out = open("new.txt", "wt") for line in open("old.txt") out.write(line.replace('I', 'You')) 請問我要如何寫才能同時將 I、is 換成 You、are,然後存到new.txt。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.32.227.107

11/15 17:20, , 1F
write(line.replace('I','You').replace('is','are')) ?
11/15 17:20, 1F

11/15 17:21, , 2F
雖然我比較好奇為什麼 I -> You, 不是 am -> are 囧"
11/15 17:21, 2F

11/15 17:32, , 3F
呵呵~ 可以了 感謝!!
11/15 17:32, 3F
文章代碼(AID): #1Gf9gFp2 (Python)
文章代碼(AID): #1Gf9gFp2 (Python)