鱼C论坛

 找回密码
 立即注册
查看: 1535|回复: 4

[已解决]小甲鱼老师30讲4题答案在我这运行错误,请问什么原因?

[复制链接]
发表于 2017-7-3 12:31:11 | 显示全部楼层 |阅读模式
10鱼币
错误原因:
for each_line in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence
“gbk”编解码器无法在位置4中解码字节0x80:非法多字节序列
错误位置:
第三个def函数【def search_in_file(file_name,key)】第6行

下面是第30讲第4题答案:

  1. import os

  2. def print_pos(key_dict):
  3.     keys = key_dict.keys()
  4.     keys = sorted(keys) # 由于字典是无序的,我们这里对行数进行排序
  5.     for each_key in keys:
  6.         print('关键字出现在第 %s 行,第 %s 个位置。' % (each_key, str(key_dict[each_key])))


  7. def pos_in_line(line, key):
  8.     pos = []
  9.     begin = line.find(key)
  10.     while begin != -1:
  11.         pos.append(begin + 1) # 用户的角度是从1开始数
  12.         begin = line.find(key, begin+1) # 从下一个位置继续查找

  13.     return pos


  14. def search_in_file(file_name, key):
  15.     f = open(file_name)
  16.     count = 0 # 记录行数
  17.     key_dict = dict() # 字典,用户存放key所在具体行数对应具体位置
  18.     print('测试一')
  19.    [color=Red] for each_line in f:[/color]
  20.         count += 1
  21.         print('测试三')
  22.         if key in each_line:
  23.             pos = pos_in_line(each_line, key) # key在每行对应的位置
  24.             key_dict[count] = pos
  25.     print('测试二')
  26.     f.close()
  27.     return key_dict


  28. def search_files(key, detail):   
  29.     all_files = os.walk(os.getcwd())
  30.     txt_files = []

  31.     for i in all_files:
  32.         for each_file in i[2]:
  33.             if os.path.splitext(each_file)[1] == '.txt': # 根据后缀判断是否文本文件
  34.                 each_file = os.path.join(i[0], each_file)
  35.                 txt_files.append(each_file)
  36.    

  37.     for each_txt_file in txt_files:
  38.         key_dict = search_in_file(each_txt_file, key)
  39.         if key_dict:
  40.             print('================================================================')
  41.             print('在文件【%s】中找到关键字【%s】' % (each_txt_file, key))
  42.             if detail in ['YES', 'Yes', 'yes']:
  43.                 print_pos(key_dict)


  44. key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  45. detail = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' % key)
  46. search_files(key, detail)
复制代码
最佳答案
2017-7-3 12:31:12
编码问题
open的时候要正确指定编码
参考我这个回答
http://bbs.fishc.com/forum.php?mod=viewthread&tid=89913&page=1#pid2934373
TIM截图20170703121956.jpg

最佳答案

查看完整内容

编码问题 open的时候要正确指定编码 参考我这个回答 http://bbs.fishc.com/forum.php?mod=viewthread&tid=89913&page=1#pid2934373
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-3 12:31:12 | 显示全部楼层    本楼为最佳答案   
编码问题
open的时候要正确指定编码
参考我这个回答
http://bbs.fishc.com/forum.php?mod=viewthread&tid=89913&page=1#pid2934373
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-7-3 13:20:51 | 显示全部楼层
sky 发表于 2017-7-3 12:56
编码问题
open的时候要正确指定编码
参考我这个回答

解决了,谢谢。

  1. file = open(file_name,encoding='utf-8')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-3 13:24:42 | 显示全部楼层

我还是推荐你看看那个回答里的链接中的文章
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-7-3 13:25:34 | 显示全部楼层
sky 发表于 2017-7-3 13:24
我还是推荐你看看那个回答里的链接中的文章

已经看完了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 05:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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