|  | 
 
| 
第29件第4题,我已经跟答案差不多了,就是跑不答案来
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  测了几下发现是
 for eachline in file:
 if old in eachline:
 count+=eachline.count(old)
 eachline.replace(old,new)
 中的replace函数并没将eachline里的旧词替换
 为什么?????????
 
 
 
 
 
 def replace_all(file_name,old,new):
 file=open(file_name)
 count=0
 content=[]
 for eachline in file:
 if old in eachline:
 count+=eachline.count(old)
 eachline.replace(old,new)
 content.append(eachline)
 print('文件',file_name,'中共有',count,'个【',old,'】')
 print('您确定要把所有的【',old,'】替换成【',new,'】吗?')
 b=input('【YES/NO】:')
 print(b)
 print(content)
 if b in ['yes','YES','Yes']:
 file.close()
 file_write = open(file_name, 'w')
 file_write.writelines(content)
 file_write.close()
 else:
 file.close()
 
 
 x=input('请输入文件名:')
 y=input('请输入需要替换的单词或字符:')
 z=input('请输入新的单词后字符:')
 replace_all(x,y,z)
 
replace() 不会改变原字符串,它会返回新的字符串,所以应该: 复制代码eachline = eachline.replace(old, new)
 | 
 |