鱼C论坛

 找回密码
 立即注册
查看: 3074|回复: 6

[已解决]文件系统04题

[复制链接]
发表于 2021-3-22 19:39:31 | 显示全部楼层 |阅读模式
20鱼币
本帖最后由 huangdongdong 于 2021-3-24 15:43 编辑
  1. import os

  2. def print_pos(key_dict):                                                                                                #这里的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.    
  19.     for each_line in f:
  20.         count += 1
  21.         if key in each_line:
  22.             pos = pos_in_line(each_line, key) # key在每行对应的位置
  23.             key_dict[count] = pos
  24.    
  25.     f.close()
  26.     return key_dict


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

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

  35.     for each_txt_file in txt_files:
  36.         key_dict = search_in_file(each_txt_file, key)                                                                  #这一句是什么意思
  37.         if key_dict:                                                                                                                 #这一句是什么意思
  38.             print('================================================================')
  39.             print('在文件【%s】中找到关键字【%s】' % (each_txt_file, key))
  40.             if detail in ['YES', 'Yes', 'yes']:
  41.                 print_pos(key_dict)


  42. key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  43. detail = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' % key)
  44. search_files(key, detail)
复制代码
各个函数的目标是什么

最佳答案
2021-3-22 19:39:32
  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)  #返回关键字的索引值,如果未匹配到的话,会返回-1
  10.     while begin != -1:   
  11.         pos.append(begin+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.     for each_line in f:   
  19.         count +=1
  20.         if key in each_line:  #关键字在每一行中
  21.             pos = pos_in_line(each_line,key)      
  22.             key_dict[count] = pos
  23.     f.close()
  24.     return key_dict  #返回哪一行哪个位置


  25. def search_file(key,detail):
  26.     all_files = os.walk(os.getcwd())
  27.     txt_files = []

  28.     for i in all_files:
  29.         for each_file in i[2]:
  30.             if os.path.splitext(each_file)[1] == ".txt":# 根据后缀判断是否文本文件

  31.                 each_file = os.path.join(i[0],each_file)   
  32.                 txt_files.append(each_file)
  33.     for each_txt_file in txt_files:
  34.         key_dict = search_in_file(each_txt_file,key)  #之前定义的函数 寻找每个文件,获取关键字的行,位置
  35.         if key_dict:  #如果有 ,那么接下来打印
  36.             print("===============================================")
  37.             print("在文件【%s】中找到关键字【%s】"% (each_txt_file,key))
  38.             if detail in ["YES","yes","Yes"]:
  39.                   print_pos(key_dict)

  40.                   
  41. key = input("请将该脚本放于待查找的文件夹内,请输入关键字:")
  42. detail = input("请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):"% key)
  43. search_file(key,detail)
复制代码


个人理解  欢迎指正

最佳答案

查看完整内容

个人理解 欢迎指正
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-24 15:47:09 | 显示全部楼层
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-22 19:39:32 | 显示全部楼层    本楼为最佳答案   
  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)  #返回关键字的索引值,如果未匹配到的话,会返回-1
  10.     while begin != -1:   
  11.         pos.append(begin+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.     for each_line in f:   
  19.         count +=1
  20.         if key in each_line:  #关键字在每一行中
  21.             pos = pos_in_line(each_line,key)      
  22.             key_dict[count] = pos
  23.     f.close()
  24.     return key_dict  #返回哪一行哪个位置


  25. def search_file(key,detail):
  26.     all_files = os.walk(os.getcwd())
  27.     txt_files = []

  28.     for i in all_files:
  29.         for each_file in i[2]:
  30.             if os.path.splitext(each_file)[1] == ".txt":# 根据后缀判断是否文本文件

  31.                 each_file = os.path.join(i[0],each_file)   
  32.                 txt_files.append(each_file)
  33.     for each_txt_file in txt_files:
  34.         key_dict = search_in_file(each_txt_file,key)  #之前定义的函数 寻找每个文件,获取关键字的行,位置
  35.         if key_dict:  #如果有 ,那么接下来打印
  36.             print("===============================================")
  37.             print("在文件【%s】中找到关键字【%s】"% (each_txt_file,key))
  38.             if detail in ["YES","yes","Yes"]:
  39.                   print_pos(key_dict)

  40.                   
  41. key = input("请将该脚本放于待查找的文件夹内,请输入关键字:")
  42. detail = input("请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):"% key)
  43. search_file(key,detail)
复制代码


个人理解  欢迎指正
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-25 15:11:08 | 显示全部楼层
代码小白liu 发表于 2021-3-24 22:37
个人理解  欢迎指正

第一个函数的目标是打印结果,第二个函数的目标是查找关键字在单行中的位置,第三个函数的目标是查找关键字在单个文本中行的位置,第四个函数的目标是查找关键字在文件夹内文本的位置,是这样理解的吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-25 15:53:42 | 显示全部楼层
huangdongdong 发表于 2021-3-25 15:11
第一个函数的目标是打印结果,第二个函数的目标是查找关键字在单行中的位置,第三个函数的目标是查找关键 ...

就是这样,每个函数就是细分
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-25 16:24:02 | 显示全部楼层
代码小白liu 发表于 2021-3-25 15:53
就是这样,每个函数就是细分

  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 = []#存放key在line中的位置
  9.     begin = line.find(key)#返回关键字的索引值,如果未匹配到的话,会返回-1
  10.     while begin != -1:#如果该行包含key
  11.         pos.append(begin + 1) # 用户的角度是从1开始数(因为python从0开始)
  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.    
  19.     for each_line in f:
  20.         count += 1
  21.         if key in each_line:
  22.             pos = pos_in_line(each_line, key) # key在每行对应的位置
  23.             key_dict[count] = pos#将pos作为key_dict[count]的键值
  24.    
  25.     f.close()
  26.     return key_dict#返回哪一行哪个位置


  27. def search_files(key, detail):#统计关键字在文建中的位置   
  28.     all_files = os.walk(os.getcwd())#返回当前工作目录中的三元组(路径,包含的目录,包含的文件)
  29.     txt_files = []#储存合成的路径

  30.     for i in all_files:#遍历三元组
  31.         for each_file in i[2]:#遍历三元组中的包含文件
  32.             if os.path.splitext(each_file)[1] == '.txt': # 根据后缀判断是否文本文件
  33.                 each_file = os.path.join(i[0], each_file)#将三元组中的路径和文件合成一个路径
  34.                 txt_files.append(each_file)#将合成的路径添加

  35.     for each_txt_file in txt_files:#遍历合成的路径
  36.         key_dict = search_in_file(each_txt_file, key)#调用上一个函数
  37.         if key_dict:#如果路径内有关键字,则打印出
  38.             print('================================================================')
  39.             print('在文件【%s】中找到关键字【%s】' % (each_txt_file, key))
  40.             if detail in ['YES', 'Yes', 'yes']:
  41.                 print_pos(key_dict)


  42. key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  43. detail = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' % key)
  44. search_files(key, detail)
复制代码
这是我写的注释,望能够指正?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-27 15:00:56 | 显示全部楼层
huangdongdong 发表于 2021-3-25 16:24
这是我写的注释,望能够指正?

差不多就这样,自己能看到每一步就行,就是套娃
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 00:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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