鱼C论坛

 找回密码
 立即注册
查看: 2306|回复: 2

打印文档提示解码错误,救助各位同学帮忙解决下

[复制链接]
发表于 2017-6-21 03:22:59 | 显示全部楼层 |阅读模式

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

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

x
  1. #!/usr/bin/env python
  2. #_*_ coding:utf-8 _*_

  3. '''
  4. 在指定目录中查找包含关键字的文件

  5. Version 1.0
  6. Author xpli
  7. '''

  8. import os

  9. def print_key_position_detail(key_dict):
  10.         keys = key_dict.keys()
  11.         keys = sorted(keys)
  12.         for each_key in keys:
  13.                 print("关键字出现在第%d行,第%s个位置。"% (each_key, str(key_dict[each_key])))

  14. def search_key_line(line, key):
  15.         position = []
  16.         begin = line.find(key)
  17.         while begin != -1:
  18.                 position.append(begin + 1)
  19.                 begin = line.find(key, begin+1)
  20.         return position

  21. def search_key_doc(doc_name, key):
  22.         count = 0
  23.         f = open(doc_name,"r", encoding = "ascii")
  24.         key_dict = dict()
  25.         for each_line in f:
  26.                 count += 1
  27.                 pos = search_key_line(each_line, key)
  28.                 key_dict[count]= pos
  29.         return key_dict
  30.         f.close()



  31. def search_file(file_path, key, detail):
  32.         os.chdir(file_path)
  33.         listdir = os.listdir(os.curdir)
  34.         doclist = []
  35.         for each_file in listdir:
  36.                 if each_file == "System Volume Information":
  37.                         continue
  38.                 if os.path.isdir(each_file):
  39.                         search_file(each_file, key, detail)
  40.                         os.chdir(os.pardir)
  41.                 if os.path.splitext(each_file)[1] == ".txt":
  42.                         doc_position = os.getcwd() + os.sep + each_file
  43.                         doclist.append(doc_position)
  44.         for each in doclist:
  45.                 key_dict = search_key_doc(each, key)
  46.                 if key_dict:
  47.                         print ("=========================================")
  48.                         print ("在文件【%s】中找到关键字【%s】" % (each, key))
  49.                         if detail in ["YES", "yes", "Yes"]:
  50.                                 print_key_position_detail(key_dict)

  51. file_path = input("请输入要查找关键字的路径:")
  52. key = input("请输入要查找的关键字:")
  53. detail = input("请确认是否需要打印关键字【%s】所在位置的详细信息(YES/NO):" % key)

  54. search_file(file_path, key, detail)
  55.                
复制代码


执行上面这段代码总是解决错误。查找了半天百度也没有解决,求助各位同学帮忙解决下:
问题如下:
Current directory: F:\
请将该脚本放于待查找的文件夹内,请输入关键字:新版本
请问是否需要打印关键字【新版本】在文件中的具体位置(YES/NO):yes
Traceback (most recent call last):
  File "new 1.py", line 56, in <module>
    search_files(key, detail)
  File "new 1.py", line 46, in search_files
    key_dict = search_in_file(each_txt_file, key)
  File "new 1.py", line 25, in search_in_file
    for each_line in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 8: illegal multibyte sequence
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-6-21 08:43:36 | 显示全部楼层
  1. f = open(doc_name,"r", encoding = "ascii")
复制代码

后面应该是utf-8吧。还没见过后面用ascii的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-21 09:24:12 | 显示全部楼层
search_in_file(each_txt_file, key)

这个函数在那里呀?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 09:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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