记事本++多重搜索

2024-03-29 07:12:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Notepad++的“在文件中查找”(Ctrl+Shift+F)功能在文本文件中搜索字符串的出现。 我的文本文件(.sql)位于目录的子文件夹中(例如下面的“视图”和“具体化视图”):

C:\users\Schema Export\schema_name\Views  
C:\users\Schema Export\schema_name\ MaterializedViews

例如,在记事本的“Find result”屏幕中,单击“Find All”即可看到“cust_account”(目录:“C:\users\Schema Export\Schema_name\”):

^{pr2}$

例如,对“pos_row_id”的新搜索结果显示:

C:\users\Schema Export\schema_name\Views\V_ACCOUNT.sql (2 hits)  
C:\users\Schema Export\schema_name\Views\V_ACCOUNT_rework.sql (2 hits)  
C:\users\Schema Export\schema_name\Views\V_HUB_REFER.sql (1 hit)  

在折叠模式下,我现在在搜索窗口中得到两行:

Search "pos_row_id" (5 hits in 3 files)  
Search "cust_account" (22 hits in 4 files)

我想自动进行搜索,只需一次就可以完成如下操作:

Search value1 (x1 hits in y1 files), where value1 is "pos_row_id" in example above,  
Search value2 (x2 hits in y2 files), where value2 is "cust_account" in example above,  
Search value3 (x3 hits in y3 files), etc.  
Search value4 (x4 hits in y4 files) 
Search value5 (x5 hits in y5 files) 
Search value6 (x6 hits in y6 files)
(..)

我的第一个想法是录制一个宏并更新它(用我的不同值、value1、value2等复制现有的代码)
不幸的是,宏代码不是在快捷方式.xml“文件(此主题有许多线程…)

有没有其他的可能性(也许用python脚本)从上面描述的所有搜索结果的值列表中一次性检索出来?在

谢谢!在


Tags: nameinpossqlsearchschemaexportaccount
1条回答
网友
1楼 · 发布于 2024-03-29 07:12:43

使用python3.6.2。我首先将值存储到dict中,然后将打印时的命中数相加。在

import os
from collections import defaultdict


def find_all(*args):
    matches = defaultdict(dict)
    for subdir, dirs, files in os.walk('.'):  # walk through all files in all subdirectories
        for file in files:
            with open(os.path.join(subdir, file)) as f:
                data = f.read()
                for arg in args:
                    c = data.count(arg)  # count the number of hits in this file
                    if c > 0:  # if positive, add it to the matches
                        matches[arg][os.path.join(subdir, file)] = c

    return matches


search = find_all('test', 'file')  # your search keys here
for key in search:
    print(f"Search {key} ({sum(search[key].values())} hits in {len(search[key])} files),")

'test''file'只是随机的搜索键,我在函数中插入它们来测试它,用你的搜索项修改它们。您还可以将iterable传递给函数,例如要搜索的单词list。在

相关问题 更多 >