鱼C论坛

 找回密码
 立即注册
查看: 678|回复: 4

[已解决]python第30讲课后作业,查找字符串并输出行号和列号,为什么我写的代码不能print

[复制链接]
发表于 2020-3-28 16:40:17 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 qiuyouzhi 于 2020-3-28 17:02 编辑

问题:请问能不能帮我看一下 row_column()函数没有输出任何文件
思路:1.先建立一个函数查找目录下所有的txt文件(包括文件夹里面的)文件名存储在temp列表里
2. 第二个函数,row_column()先遍历每个txt文件,看看目标字符串是不是在这个文件里面,如果在里面遍历每一行,用count统计行号,用index统计列号
3.运行后,第二个函数完全没有执行,不知道哪里出了问题
谢谢
运行结果:

运行结果

运行结果

代码
  1. import os

  2. def search_file():
  3.     os.curdir
  4.     a = os.getcwd()
  5.    
  6.     for each_file in os.listdir(os.curdir):
  7.         
  8.         if os.path.splitext(each_file)[1] == '.txt':  #查找出目录下所有的.txt文件
  9.             temp.append(each_file)
  10.          
  11.         if os.path.isdir(each_file):
  12.             search_file(each_file)
  13.             os.chdir(os.pardir)
  14.             
  15. def row_column():             #确定行号和列号
  16.    
  17.     for each_file1 in temp:     #调用在列表temp下保持的每个txt文件
  18.         
  19.         f = open(str(each_file1))
  20.         lines = f.readlines()
  21.         for each_row in lines:   #每个txt文件的每一行
  22.            
  23.             if target not in each_row:
  24.                 count += 1
  25.             else:
  26.                 pos[count] = each_row.index(target) #把列号保存在列表pos里
  27.                
  28.                 print('========')
  29.                 print('在文件'+ each_file1 + '中找到关键字' + target)
  30.                 for count1 in pos.keys():  #输出每一个包含target字符串的行和列
  31.                     print('关键字出现在第:' + count + '行,第' + post[count]+ '个位置')
  32.                 count += 1
  33.         f.close()
  34. count = 0
  35. pos = {}                    
  36. temp = []   
  37. lines = []
  38. a =()
  39. target = input('请输入要查找的字符串:')
  40. check = input('是否打印关键字具体位置yes/no:')
  41. if check == 'yes':
  42.     search_file()
  43.     row_column()
复制代码

最佳答案
2020-3-28 16:54:55
先吐槽一句:好多全局变量,为什么不做参数传递到函数

你先检查一下temp变量在search_file函数后存的元素是不是每一个txt文件
然后检查打开的文件中的内容
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-28 16:54:55 | 显示全部楼层    本楼为最佳答案   
先吐槽一句:好多全局变量,为什么不做参数传递到函数

你先检查一下temp变量在search_file函数后存的元素是不是每一个txt文件
然后检查打开的文件中的内容
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-28 17:10:50 | 显示全部楼层
BngThea 发表于 2020-3-28 16:54
先吐槽一句:好多全局变量,为什么不做参数传递到函数

你先检查一下temp变量在search_file函数后存的元 ...

第一次没有什么经验,我先去改一下,参数好像是有问题。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-2 18:02:20 | 显示全部楼层
这个是我修改后的代码,可以实现的,但是有些地方比较乱,供参考


  1. import os

  2. def search_file(start_dir):
  3.     os.chdir(start_dir)
  4.    
  5.    
  6.     for each_file in os.listdir(os.curdir):
  7.         
  8.         if os.path.splitext(each_file)[1] == '.txt':#查找出目录下所有的.txt文件
  9.             each_file = os.path.join(os.getcwd(),each_file)
  10.             temp.append(each_file)
  11.          
  12.         if os.path.isdir(each_file):
  13.             search_file(each_file)
  14.             os.chdir(os.pardir)


  15. def pos_in_line(each_row,target):   #查找每一行
  16.     column = []
  17.     begin = each_row.find(target)
  18.     while begin != -1:
  19.         column.append(begin + 1)
  20.         begin = each_row.find(target, begin + 1)
  21.         

  22.     return column


  23.             
  24. def row_column(target):             #确定行号和列号
  25.    
  26.     for each_file_1 in temp:     #调用在列表temp下保持的每个txt文件
  27.         
  28.         f = open(each_file_1)
  29.         count = 0
  30.         pos = {}
  31.         txt = []
  32.         
  33.         lines = f.readlines()
  34.         for each_row in lines:   #每个txt文件的每一行
  35.             count += 1  
  36.             if target in each_row:
  37.                 pos[count] = pos_in_line(each_row,target) #把列号保存在字典pos里
  38.                 txt.append(each_file_1)
  39.         news_txt = []
  40.         for direction in txt:        #列表去重
  41.             if direction not in news_txt:
  42.                 news_txt.append(direction)        

  43.         for i in news_txt:  #打印
  44.             print('========')
  45.             print('在文件'+ i + '中找到关键字' + target)
  46.             for count1 in pos.keys():  #输出每一个包含target字符串的行和列
  47.             
  48.                 print('关键字出现在第:%s行,第%s个位置' %(count1,str(pos[count1])))
  49.             
  50.         f.close()
  51.         
  52.         


  53.      

  54.                   
  55. temp = []   
  56. lines = []
  57. target = input('请输入要查找的字符串:')
  58. check = input('是否打印关键字具体位置yes/no:')
  59. if check == 'yes':
  60.     search_file(os.getcwd())
  61.     row_column(target)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-2 18:10:20 | 显示全部楼层
BngThea 发表于 2020-3-28 16:54
先吐槽一句:好多全局变量,为什么不做参数传递到函数

你先检查一下temp变量在search_file函数后存的元 ...

temp 变量我忽略了路径。。。所以最开始temp只保存了文件名,没有路径名。
全局变量我已经改写成局部变量了,不过整体代码还是冗余,不够优美。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-3 18:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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