鱼C论坛

 找回密码
 立即注册
查看: 2590|回复: 6

[已解决]29讲 课后作业 第4题 不知道哪里出错了,求解答

[复制链接]
发表于 2016-11-4 11:45:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目是:编写一个程序实现全部替换功能


  1. 请输入文件名:something2.txt
  2. 请输入需要替换的单词或字符:愿
  3. 请输入新的单词或字符:希望
  4. 文件something2.txt中共有4个愿
  5. 您确定要把所有的【愿】替换为【希望】吗?
  6. 【yes/no】:yes
复制代码


我写的小程序如下:
  1. def replace(file_name, word, re_word):

  2.     f = open(file_name, '+a')
  3.     f.seek(0,0)
  4.     text= f.read()
  5.     count=0  #存储文件内输入字符的个数

  6.     for each in text:
  7.         if each == word :
  8.             count+=1
  9.     print('文件%s中共有%d个%s'%(file_name,count,word))
  10.     print('您确定要把所有的【%s】替换为【%s】吗?'%(word,re_word))

  11.    
  12.     answer=input('【yes/no】:')
  13.     f.seek(0,0)
  14.     for each in text:
  15.         if each==word:
  16.             if answer == 'yes':
  17.                 each = re_word

  18.     f.close()

  19.    


  20. file_name= input('请输入文件名:')
  21. word=input('请输入需要替换的单词或字符:')
  22. re_word=input('请输入新的单词或字符:')
  23. replace(file_name,word,re_word)


  24.    
复制代码


但是最后打开文件发现并没有替换成功
最佳答案
2016-11-7 18:05:14
按你的思路我改写了。
  1. def replace(file_name, word, re_word):

  2.     f = open(file_name, encoding = 'utf-8')
  3.     text = list(f)
  4.     f.close()
  5.     count=0  #存储文件内输入字符的个数

  6.     for each in text:
  7.         if word in each:
  8.             count+=1
  9.     print('文件%s中共有%d个%s'%(file_name,count,word))
  10.     print('您确定要把所有的【%s】替换为【%s】吗?'%(word,re_word))

  11.    
  12.     answer=input('【yes/no】:')
  13.     if answer == 'yes':
  14.         text1 = []
  15.         for each in text:
  16.             each = each.replace(word, re_word)
  17.             text1.append(each)
  18.     f = open(file_name, 'w', encoding = 'utf-8')
  19.     f.writelines(text1)
  20.     f.close()

  21.    


  22. file_name= input('请输入文件名:')
  23. word=input('请输入需要替换的单词或字符:')
  24. re_word=input('请输入新的单词或字符:')
  25. replace(file_name,word,re_word)


  26.    
复制代码

题目要求

题目要求

something2.txt

258 Bytes, 下载次数: 4

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-4 21:16:21 | 显示全部楼层
你的程序没有写入过程呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-7 11:11:50 | 显示全部楼层
冬雪雪冬 发表于 2016-11-4 21:16
你的程序没有写入过程呀

加上一句写入的语句,还是没有实现替换,text的内容没有替换,是替换的那个for语句有问题么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-7 18:05:14 | 显示全部楼层    本楼为最佳答案   
按你的思路我改写了。
  1. def replace(file_name, word, re_word):

  2.     f = open(file_name, encoding = 'utf-8')
  3.     text = list(f)
  4.     f.close()
  5.     count=0  #存储文件内输入字符的个数

  6.     for each in text:
  7.         if word in each:
  8.             count+=1
  9.     print('文件%s中共有%d个%s'%(file_name,count,word))
  10.     print('您确定要把所有的【%s】替换为【%s】吗?'%(word,re_word))

  11.    
  12.     answer=input('【yes/no】:')
  13.     if answer == 'yes':
  14.         text1 = []
  15.         for each in text:
  16.             each = each.replace(word, re_word)
  17.             text1.append(each)
  18.     f = open(file_name, 'w', encoding = 'utf-8')
  19.     f.writelines(text1)
  20.     f.close()

  21.    


  22. file_name= input('请输入文件名:')
  23. word=input('请输入需要替换的单词或字符:')
  24. re_word=input('请输入新的单词或字符:')
  25. replace(file_name,word,re_word)


  26.    
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-7 19:11:03 | 显示全部楼层
冬雪雪冬 发表于 2016-11-7 18:05
按你的思路我改写了。

多谢冬雪,我看过下面的参考答案之后,发现for循环下面用replace函数比较有效,谢谢你的解答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-10 09:56:48 | 显示全部楼层
请问楼主用的什么编辑工具写的python,用自带的吗,感觉不好用啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-10 10:57:29 | 显示全部楼层
jll421740865 发表于 2016-11-10 09:56
请问楼主用的什么编辑工具写的python,用自带的吗,感觉不好用啊。

我是初学者,用IDLE足够了,基本的python知识掌握了,再考虑换其他的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-30 03:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表