鱼C论坛

 找回密码
 立即注册
查看: 2416|回复: 5

Python 30讲课后4题

[复制链接]
发表于 2021-5-12 17:38:10 | 显示全部楼层 |阅读模式

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

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

x
  1. import os
  2. def find_fun(path, word):
  3.     i = 0
  4.     count = []
  5.     for each in os.listdir(path):
  6.         each_path = os.path.join(path, each)
  7.         if os.path.isfile(each_path):
  8.             each_list = os.path.splitext(each)
  9.             if str(each_list[1]) == '.txt':
  10.                 print('=================================================')
  11.                 print(f'在文件【{each_path}】中找到关键字【{word}】')
  12.                 fun(each_path, word)
  13.         else:
  14.             next_path = each_path
  15.             find_fun(next_path, word)

  16. def fun(each_path, word):
  17.     i = 0
  18.     count = []
  19.     file = open (each_path)
  20.     file_list = list(file)
  21.     for each_line in file_list:
  22.         i += 1
  23.         index = each_line.find(word)
  24.         if index != -1:
  25.             count.append(index)
  26.             for m in range(count[-1], len(each_line)):
  27.                 if index != -1:
  28.                     index = each_line.find(word, (count[-1] + 1), len(each_line))
  29.             print(f'关键字出现在第 {i} 行,第{count}位置')
  30.             count.clear()

  31. word = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  32. path = os.getcwd()
  33. print (f'请问是否需要打印关键字【{word}】在文件中的具体位置(YES\\NO):', end = '')
  34. judge = input()
  35. if judge == 'YES' or judge == 'yes':
  36.     find_fun(path, word)
  37.    
复制代码




运行报错编码错误,什么叫编码错误,怎么改正
路径有中文字的问题吗

还有什么其他问题吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-12 17:56:06 | 显示全部楼层
第20行
  1. file = open(each_path, encoding='utf-8')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-12 18:06:43 | 显示全部楼层

试了 ,不行

好像是因为我的目标文件有点是utf8 编码  有的是gbk编码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-12 18:20:46 | 显示全部楼层
如果是用pycharm编写的话,可以试试到文件--设置--编辑器--文件编码,你会看左上两个和下面一个框写着gbk,把这三个改成utf-8。或者你把报错的代码发一下。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-12 18:38:28 | 显示全部楼层
  1. import os
  2. def find_fun(path, word):
  3.     for each in os.listdir(path):
  4.         each_path = os.path.join(path, each)
  5.         if os.path.isfile(each_path):
  6.             each_list = os.path.splitext(each)
  7.             if str(each_list[1]) == '.txt':
  8.                 file = open (each_path)
  9.                 if word in file.read():
  10.                     file.close()
  11.                     print('=================================================')
  12.                     print(f'在文件【{each_path}】中找到关键字【{word}】')
  13.                     fun(each_path, word)
  14.         else:
  15.             next_path = each_path
  16.             find_fun(next_path, word)

  17. def fun(each_path, word):
  18.     i = 0
  19.     count = []
  20.     file = open (each_path)
  21.     file_list = list(file)
  22.     for each_line in file_list:
  23.         i += 1
  24.         index = each_line.find(word)
  25.         if index != -1:
  26.             count.append(index)
  27.             for m in range(count[-1], len(each_line)):
  28.                 if index != -1:
  29.                     index = each_line.find(word, (count[-1] + 1), len(each_line))
  30.             print(f'关键字出现在第 {i} 行,第{count}位置')
  31.             count.clear()
  32.     file.close()

  33. word = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  34. path = os.getcwd()
  35. print (f'请问是否需要打印关键字【{word}】在文件中的具体位置(YES\\NO):', end = '')
  36. judge = input()
  37. if judge == 'YES' or judge == 'yes':
  38.     find_fun(path, word)
  39.    
复制代码


已解决了,试过gbk 和 utf8 编码方式打开,都会报错
后来在群小伙伴的提示下发现我的目标文件有点是gbk编码 有的是utf8编码
这样就导致无论用哪种方式打开总会报编码错误的BUG
最后把所有文件全部修改为gbk编码方式就可以完美运行了
如果大家发现还有BUG请在后面跟帖讨论
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-12 20:51:33 | 显示全部楼层
sxhqyxc 发表于 2021-5-12 18:38
已解决了,试过gbk 和 utf8 编码方式打开,都会报错
后来在群小伙伴的提示下发现我的目标文件有点是gb ...

那你可以用chardet
  1. import chardet
  2. with open('test.txt', 'rb') as f:
  3.     cd = chardet.detect(f.read())['encoding']
  4. with open('test.txt', 'r', encoding=cd) as f:
  5.     print(f.read())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 01:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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