码迷,mamicode.com
首页 > 编程语言 > 详细

《Python编程快速上手》第8.9.2实践练习

时间:2018-02-12 15:14:23      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:put   read   pre   练习   utf-8   imp   lang   修改   nts   

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实践练习

标签:put   read   pre   练习   utf-8   imp   lang   修改   nts   

原文地址:http://blog.51cto.com/2884868/2071347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!