鱼C论坛

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

关于课后作业30的最后一题

[复制链接]
发表于 2019-12-26 12:39:15 | 显示全部楼层 |阅读模式

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

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

x
学了easygui想改写一下,大佬看看哪些地方有问题,我晕了!
import os
import easygui as g
linedict={}
posdict={}
posinline=[]
pos=[]
count=0
text_files=[]
def show_result(path,keyword):
    text=''
    for i in linedict:
        for j in linedict[i]:
            text+='在文件【%s】中找到关键字%s\n关键字出现在第%s行,第%s个位置'%(i,keyword,j,posdict[i])
    msg='结果如下'
    titile='统计结果'
    g.textbox(msg,titile,text)

def search_line(filename,keyword):
    for each_textfile in text_files:
        with open(each_textfile) as f:
            try:
                if keyword in f.read():
                    for eachline in f:
                        count+=1
                        if keyword in eachline:
                            posinline.append(count)
                            search_pos(filename,eachline,keyword)
                    linedict[filename]=posinline
            except UnicodeDecodeError:
                pass
def search_pos(filename,eachline,keyword):
    begin=eachline.find(keyword)
    while begin!=-1:
        pos.append(begin+1)
        begin=eachline.find(keyword,begin+1)
    posdict[filename]=pos            
                    
def search_file(path,keyword):
    allfiles=os.walk(path)
    for k in allfiles:
        for eachfile in k[2]:
            if os.path.splitext(eachfile)[1]=='.txt':
                eachfile=os.path.join(k[0],eachfile)
                text_files.append(eachfile)
                search_line(eachfile,keyword)
               
        

keyword=g.enterbox(msg='请输入关键字')
path=g.diropenbox()
search_file(path,keyword)
show_result(path,keyword)

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-26 14:26:30 | 显示全部楼层
如何正确地发代码、上传图片和附件?
https://fishc.com.cn/thread-52272-1-1.html
(出处: 鱼C论坛)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-26 16:37:01 | 显示全部楼层
本帖最后由 阿匠 于 2019-12-26 16:38 编辑
  1. import os
  2. import easygui as g
  3. def searchfile(path):
  4.     os.chdir(path)
  5.     for eachfile in os.listdir(os.curdir):
  6.         if os.path.splitext(eachfile)[1]=='.txt':
  7.             textfiles.append(os.getcwd()+os.sep+eachfile)
  8.         if os.path.isdir(eachfile):
  9.             searchfile(eachfile)
  10.             os.chdir(os.pardir)

  11. def searchline(filename,keyword):
  12.     count=0
  13.     dict1={}
  14.     with open(filename) as f:
  15.         for eachline in f:
  16.             count+=1
  17.             if keyword in eachline:
  18.                 pos=searchpos(eachline,keyword)
  19.                 dict1[count]=pos
  20.     return dict1
  21. def searchtxt(keyword):
  22.     text=''
  23.     for each_textfile in textfiles:
  24.         with open(each_textfile) as f:
  25.             if keyword in f.read():
  26.                 text+='===============================\n在%s找到关键字%s\n'%(each_textfile,keyword)
  27.                 dict1=searchline(each_textfile,keyword)
  28.                 keys=dict1.keys()
  29.                 keys=sorted(keys)
  30.                 for eachkey in keys:
  31.                     for i in dict1[eachkey]:
  32.                         text+='关键字出现在第%s行,第%s位置\n'%(eachkey,i)
  33.     return text
  34. def searchpos(eachline,keyword):
  35.     pos=[]
  36.     begin=eachline.find(keyword)
  37.     while begin!=-1:
  38.         pos.append(begin+1)
  39.         begin=eachline.find(keyword,begin+1)
  40.     return pos
  41.             
  42.         


  43.   
  44. textfiles=[]      
  45. keyword=g.enterbox(msg='输入关键字')
  46. path=g.diropenbox()
  47. searchfile(path)
  48. msg=''
  49. title=''
  50. text=searchtxt(keyword)
  51. if text!='':
  52.     g.textbox('位置如下','寻找关键字',text)
  53. else:
  54.     g.msgbox('什么也没找到')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-21 12:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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