鱼C论坛

 找回密码
 立即注册
查看: 700|回复: 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.运行后,第二个函数完全没有执行,不知道哪里出了问题
谢谢
运行结果:

运行结果

运行结果

代码
import os

def search_file():
    os.curdir
    a = os.getcwd()
   
    for each_file in os.listdir(os.curdir):
        
        if os.path.splitext(each_file)[1] == '.txt':  #查找出目录下所有的.txt文件
            temp.append(each_file)
          
        if os.path.isdir(each_file):
            search_file(each_file)
            os.chdir(os.pardir)
            
def row_column():             #确定行号和列号
   
    for each_file1 in temp:     #调用在列表temp下保持的每个txt文件
        
        f = open(str(each_file1))
        lines = f.readlines()
        for each_row in lines:   #每个txt文件的每一行
           
            if target not in each_row:
                count += 1
            else:
                pos[count] = each_row.index(target) #把列号保存在列表pos里
                
                print('========')
                print('在文件'+ each_file1 + '中找到关键字' + target)
                for count1 in pos.keys():  #输出每一个包含target字符串的行和列
                    print('关键字出现在第:' + count + '行,第' + post[count]+ '个位置')
                count += 1
        f.close()
count = 0
pos = {}                    
temp = []    
lines = []
a =()
target = input('请输入要查找的字符串:')
check = input('是否打印关键字具体位置yes/no:')
if check == 'yes':
    search_file()
    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 | 显示全部楼层
这个是我修改后的代码,可以实现的,但是有些地方比较乱,供参考

import os

def search_file(start_dir):
    os.chdir(start_dir)
    
   
    for each_file in os.listdir(os.curdir):
        
        if os.path.splitext(each_file)[1] == '.txt':#查找出目录下所有的.txt文件
            each_file = os.path.join(os.getcwd(),each_file)
            temp.append(each_file)
          
        if os.path.isdir(each_file):
            search_file(each_file)
            os.chdir(os.pardir)


def pos_in_line(each_row,target):   #查找每一行
    column = []
    begin = each_row.find(target)
    while begin != -1:
        column.append(begin + 1)
        begin = each_row.find(target, begin + 1)
        

    return column


            
def row_column(target):             #确定行号和列号
   
    for each_file_1 in temp:     #调用在列表temp下保持的每个txt文件
        
        f = open(each_file_1)
        count = 0
        pos = {}
        txt = []
        
        lines = f.readlines()
        for each_row in lines:   #每个txt文件的每一行
            count += 1  
            if target in each_row:
                pos[count] = pos_in_line(each_row,target) #把列号保存在字典pos里
                txt.append(each_file_1)
        news_txt = []
        for direction in txt:        #列表去重
            if direction not in news_txt:
                news_txt.append(direction)        

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


     

                   
temp = []    
lines = []
target = input('请输入要查找的字符串:')
check = input('是否打印关键字具体位置yes/no:')
if check == 'yes':
    search_file(os.getcwd())
    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-6-15 20:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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