免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4710 | 回复: 0
打印 上一主题 下一主题

提示keyerror [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-06 17:42 |只看该作者 |倒序浏览
本帖最后由 shaogang428 于 2010-11-06 17:56 编辑

使用下列脚本的搜索功能的时候会提示KeyError,麻烦给我检查下。谢谢。
  1. #coding:gbk
  2. import sys,cmd
  3. from cdctools import *

  4. class PyCDC(cmd.Cmd):
  5.     def __init__(self):
  6.         cmd.Cmd.__init__(self)
  7.         self.cdrom = 'h:\\My Dropbox\\python\\'
  8.         self.dir = 'd:\\Python\\CD2\\'
  9.         self.prompt = "(PyCDC)>"
  10.         self.intro = '''PyCDC0.5 使用说明:
  11.     dir 目录名     #指定保存和搜索目录,默认是 "cdc"
  12.     walk 文件名    #指定光盘信息文件名,使用 "*.cdc"
  13.     find 关键词    #使用在保存和搜索目录中遍历所有.cdc文件,输出含有关键词的行
  14.     ?           # 查询
  15.     EOF         # 退出系统,也可以使用Crtl+D(Unix)|Ctrl+Z(Dos/Windows)
  16.     '''
  17.     def help_EOF(self):
  18.         print "退出程序,Quit the program"
  19.     def do_EOF(self,line):
  20.         sys_exit()

  21.     def help_walk(self):
  22.         print "扫描文件目录 walkcd and export into *.cdc"
  23.     def do_walk(self,filename):
  24.         if filename == "":filename = raw_input("输入cdc文件名::")
  25.         print "扫描光盘内容保存到:'%s'"% filename
  26.         cdWalker(self.cdrom,self.dir+filename)

  27.     def help_dir(self):
  28.         print "指定保存/搜索目录"
  29.     def do_dir(self,pathname):
  30.         if pathname == "":pathname = raw_input("输入指定保存/搜索目录:")
  31.         self.dir = pathname
  32.         print "指定保存/搜索目录:'%s';默认是:'%s'"%(pathname,self.dir)

  33.     def help_find(self):
  34.         print "搜索关键词"
  35.     def do_find(self,keyword):
  36.         if keyword == "":keyword = raw_input("输入搜索关键词:")
  37.         print "搜索关键词:'%s'"% keyword
  38.         cdcGrep(self.dir,keyword)


  39. if __name__ == '__main__':
  40.     cdc = PyCDC()
  41.     cdc.cmdloop()
复制代码
下列是调用的cdctools.py
  1. import os
  2. import pickle
  3. def cdWalker(cdrom,cdcfile):
  4.     export = ""
  5.     for root,dirs,files in os.walk(cdrom):
  6.         export += "\n %s;%s;%s"% (root,dirs,files)
  7.     open(cdcfile,'w').write(export)

  8. def cdcGrep(cdcpath,keyword):
  9.     '''光盘信息文本关键词搜索函式
  10.     @note: 使用最简单的内置字串匹配处理来判定是否有关键词包含
  11.     @param cdcpath: 包含*.cdc 文件的目录
  12.     @param keyword: 搜索的关键词
  13.     @return: 组织匹配好的信息到字典中导出成 searched.dump 文件
  14.     @todo: 可结合搜索引擎进行模糊搜索!
  15.     '''
  16.     expDict = {}
  17.     filelist = os.listdir(cdcpath)          # 搜索目录中的文件
  18.     cdcpath=cdcpath+"/"
  19.     for cdc in filelist:                    # 循环文件列表
  20.         if os.path.isdir(cdcpath+cdc):
  21.             cdcGrep(cdcpath+cdc,keyword) # 若是子目录,则递归调用完成查找
  22.         else:
  23.             cdcfile = open(cdcpath+cdc)         # 拼合文件路径,并打开文件
  24.             for line in cdcfile.readlines():    # 读取文件每一行,并循环
  25.                 if keyword in line:             # 判定是否有关键词在行中
  26.                     #print line                  # 打印输出
  27.                     expDict[cdc].append(line)
  28.     #print expDict
  29.     pickle.dump(expDict,open("d:\\Python\\CD2\\searched.dump","w"))
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP