1. 程式人生 > >《Python編程快速上手》第8.9.2實踐練習

《Python編程快速上手》第8.9.2實踐練習

cte Language mpi hang 定義 file pytho pil 自定義

first.txt

The ADJECTIVE panada walked to the NOUN and then VERB. Anerby
NOUN was unaffected by these events.

#!python3
#-*- coding:utf-8 -*-

#8.9.2瘋狂填詞遊戲
#用戶自定義詞語,修改打開文件中的ADJECTIVE、NOUN、VERB
import re

f=open(‘first.txt‘,‘r+‘)
files=f.read()
print(files)
f.close()

changelist=[‘ADJECTIVE‘,‘NOUN‘,‘VERB‘]
for i in changelist:
    change_reg=re.compile(r‘%s‘ % i)
    text=input("Enter a %s :" % i)
    files=change_reg.sub(text,files)
fi_2=open(‘third.txt‘,‘w‘)
fi_2.write(files)
fi_2.close()

《Python編程快速上手》第8.9.2實踐練習