鱼C论坛

 找回密码
 立即注册
查看: 2851|回复: 9

课后作业29讲第四题的疑问

[复制链接]
发表于 2021-4-18 09:45:56 | 显示全部楼层 |阅读模式

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

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

x
  1. def file_replace(file_name,oldword,newword):
  2.     f = open(file_name)
  3.     count = 0
  4.     string = f.read()                       #输出为字符串
  5.     string.replace('\n','')
  6.     f.seek(0,0)

  7.     for each in string:                     #统计出现的次数
  8.         if oldword == each:
  9.             count += 1


  10.     print('文件%s中共有%s个%s' %(file_name,count,oldword))
  11.     temp = input('您确定要将所有的%s替换为%s吗? \n 【Yes\\No】' %(oldword,newword))


  12.     if temp in ['yes','YES','Yes']:        #确认是否更改
  13.         string.replace(oldword,newword)         #更改字符串
  14.         f_new = open(file_name,'w')             #重新覆盖写入
  15.         f_new.write(string)
  16.         f_new.close()
  17.    
  18.     f.close()


  19. file_name = input(r'请输入要打开的文件(C:\\test.txt):')
  20. oldword = input('请输入要替换的字符:')
  21. newword = input('请输入新的单词:')
  22. file_replace(file_name,oldword,newword)
复制代码


题目大意是将文件的某个字符更改为别的字符
改了很久,程序终于不报错了,但是文件的内容并没有更改 求解答
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-18 09:51:30 | 显示全部楼层
  1. def file_replace(file_name,oldword,newword):
  2.     f = open(file_name)
  3.     count = 0
  4.     string = f.read()                       #输出为字符串   # 这里读取完毕之后并没有关闭文件
  5.     string.replace('\n','')
  6.     f.seek(0,0)

  7.     for each in string:                     #统计出现的次数
  8.         if oldword == each:
  9.             count += 1


  10.     print('文件%s中共有%s个%s' %(file_name,count,oldword))
  11.     temp = input('您确定要将所有的%s替换为%s吗? \n 【Yes\\No】' %(oldword,newword))


  12.     if temp in ['yes','YES','Yes']:        #确认是否更改
  13.         string.replace(oldword,newword)         #更改字符串
  14.         f_new = open(file_name,'w')             #重新覆盖写入     # 这里因为上面没有关闭文件,所以打开失败
  15.         f_new.write(string)
  16.         f_new.close()
  17.    
  18.     f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-18 09:51:31 | 显示全部楼层
那是你要读的.txt文件编码问题。
.txt文件编码 有 uft-8 ansi unicode ........ 程序要识别所有编码还得要改进。默认.txt是ansi编码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-18 09:52:14 | 显示全部楼层
读取完毕之后,记得及时关闭文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-18 10:57:42 | 显示全部楼层
yuxijian2020 发表于 2021-4-18 09:52
读取完毕之后,记得及时关闭文件

py 不是自动关的么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-18 12:01:19 | 显示全部楼层

with 语句才能自动关闭
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-18 14:31:58 | 显示全部楼层
  1. def file_replace(file_name,oldword,newword):
  2.     f = open(file_name)
  3.     count = 0
  4.     string = f.read()
  5.     string.replace('\n','')
  6.     f.seek(0,0)
  7.     f.close()
  8.     for each in string:
  9.         if oldword == each:
  10.             count += 1
  11.     print('文件%s中共有%s个%s' %(file_name,count,oldword))
  12.     temp = input('您确定要将所有的%s替换为%s吗? \n 【Yes\\No】' %(oldword,newword))
  13.     if temp in ['yes','YES','Yes']:
  14.         string.replace(oldword,newword)
  15.         f_new = open(file_name,'w')
  16.         f_new.write(string)
  17.         f_new.close()
  18.    
  19.     f.close()


  20. file_name = input(r'请输入要打开的文件(C:\\test.txt):')
  21. oldword = input('请输入要替换的字符:')
  22. newword = input('请输入新的单词:')
  23. file_replace(file_name,oldword,newword)
复制代码

加了,我把4改成5,文件中还是没改,也没报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-18 14:33:10 | 显示全部楼层
本帖最后由 白本羽 于 2021-4-18 14:34 编辑
ba21 发表于 2021-4-18 09:51
那是你要读的.txt文件编码问题。
.txt文件编码 有 uft-8 ansi unicode ........ 程序要识别所有编码还得要 ...


呃,所以怎么办呢
  1. 请输入要打开的文件(C:\\test.txt):E:\\1.txt
  2. 请输入要替换的字符:4
  3. 请输入新的单词:5
  4. 文件E:\\1.txt中共有2个4
  5. 您确定要将所有的4替换为5吗?
  6. 【Yes\No】yes
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-18 16:47:37 | 显示全部楼层
白本羽 发表于 2021-4-18 14:33
呃,所以怎么办呢
  1. string.replace(oldword,newword)
复制代码


改成

  1. string = string.replace(oldword,newword)
复制代码


光string.replace 是不会改变string的内容的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-18 17:40:23 | 显示全部楼层
白本羽 发表于 2021-4-18 14:33
呃,所以怎么办呢

先    建个文件夹,自已建几个.txt测试。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 20:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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