鱼C论坛

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

[已解决]求问%s能输出列表吗?

[复制链接]
发表于 2017-11-7 10:07:16 | 显示全部楼层 |阅读模式

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

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

x
  1. #------显示文件及关键字位置------#
  2. import os
  3. def search_file(start_dir):  
  4.     os.chdir(start_dir)
  5.     all_files = os.listdir(os.curdir)
  6.     for each_file in all_files:
  7.         if os.path.isfile(each_file):
  8.             ext = os.path.splitext(each_file)[1]
  9.             if ext == '.txt':
  10.                 path = os.getcwd() + os.sep + each_file
  11.                 all_txt.append(path)    # 查找到的所有txt文件路径
  12.         if os.path.isdir(each_file):            
  13.             search_file(each_file)
  14.             os.chdir(os.pardir)
  15.     #return all_txt
  16. key = input('请将该代码放于待查找的文件夹内,请输入关键字:')     #输入关键字
  17. decide = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):'%key)   #是否打印关键字
  18. all_txt = []
  19. key_positon = []
  20. present_dir = os.getcwd()    #当前工作目录
  21. search_file(present_dir)     #调用search_file函数
  22. count = 1
  23. if decide == 'YES' or decide == 'yes':   #如果为真
  24.     for each in all_txt:   #遍历所有文件路径列表
  25.         f = open(each)
  26.         length = len(f.readlines())
  27.         f.seek(0)

  28.         for each_line in f:
  29.             if key in each_line:
  30.                 position= each_line.find(key) + 1
  31.                 key_position.append(position)              
  32.                 each_line = each_line[position:-1]
  33.                 if key in each_line:
  34.                     position = each_line.find(key) + 2
  35.                     key_position.append(position)
  36.                
  37.                
  38.                 print('在文件【%s】中找到关键字【%s】'%(each, key))
  39.                 print('关键字出现在第%d行,第%s个位置'%(count,key_position))
  40.               
  41.             count += 1
  42.             key_position = []
  43.             if count == length+1:
  44.                 count = 1
  45.         print('==================================================')         
  46.         
  47.     f.close()     
  48.    
  49.             
  50.                
复制代码

在print('关键字出现在第%d行,第%s个位置'%(count,key_position))这里,key_position是一个列表,但是可以正常运行出结果。
但是在小甲鱼的答案中,
  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:       #如果不等于-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)   #打开文件file_name
  16.     count = 0 # 记录行数
  17.     key_dict = dict() # 字典,用户存放key所在具体行数对应具体位置
  18.    
  19.     for each_line in f: #遍历每一行
  20.         count += 1     #count + 1
  21.         if key in each_line:   #如果关键字在每一行里面
  22.             pos = pos_in_line(each_line, key) # key在每行对应的位置  调用pos_in_line
  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]:   #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)     #将txt格式保存在txt_files中

  35.     for each_txt_file in txt_files:   #遍历txt_files
  36.         key_dict = search_in_file(each_txt_file, key)   #调用search_in_file
  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)   #调用函数search_files
复制代码


print('关键字出现在第 %s 行,第 %s 个位置。' % (each_key, str(key_dict[each_key]))) 这里将key_dict[each_key]转化成了字符串,我试了下,去掉str的话运行就没有输出结果。
求问这是为啥?求各位大佬解答一下
最佳答案
2017-11-7 10:14:09
是这样的  print('%s'%content)表明%s这个地方将要传入一个str类型的数据,
如果后面的content不是str类型那么就尝试转化为str,无法转化时会报错
不存在空这种情况的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-11-7 10:14:09 | 显示全部楼层    本楼为最佳答案   
是这样的  print('%s'%content)表明%s这个地方将要传入一个str类型的数据,
如果后面的content不是str类型那么就尝试转化为str,无法转化时会报错
不存在空这种情况的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-7 10:40:00 | 显示全部楼层
Teagle 发表于 2017-11-7 10:14
是这样的  print('%s'%content)表明%s这个地方将要传入一个str类型的数据,
如果后面的content不是str类型 ...

嗯嗯 谢谢~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-7 10:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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