一个简单的 python 脚本,用于“搜索整个单词的文本文件” - 使用 GUI

时间:2021-02-01 20:37:44

标签: python tkinter

我目前正在构建一个小程序,允许使用从视频剪辑中转录的文本文件在演员的对话中搜索短语。我遇到了一些问题,如下所述...

  1. 创建用户输入:

    # Get the SEARCH WINDOW
    root = tk.Tk()
    root.withdraw()
    root.option_add('*background', '#111111')
    root.option_add('*Entry*background', '#999999')
    searchPhrase = sd.askstring(
        "PhraseFinder v0.1     |     filmwerk.nyc 2021 ", "Type keyword, or entire phrase, to search...", parent=root,)>
    

    这似乎工作正常。用户输入存储在 searchPhrase...

  2. 从上面获取用户输入 (searchPhrase) 并搜索包含 800 个文本文件的目录(仅限“全字”搜索 - “忽略大小写”)。

    # Do THE SEARCH, based on user input
    import glob
    import os
    rootDir = '/Volumes/audio/TRANSCRIBE/OUT'
    os.chdir( rootDir )
    for files in glob.glob( "*.txt" ):
        with open(files) as f:
            contents = f.read()
        if (re.search(r'\b'+ re.escape(searchPhrase) + r'\b', contents, re.IGNORECASE)):
                print( f )
    

    输出:

    <_io.TextIOWrapper name='FW_A01_2020-12-01_1856_C0004.txt' mode='r' encoding='US-ASCII'>
    <_io.TextIOWrapper name='FW_A01_2020-12-01_1900_C0007.txt' mode='r' encoding='US-ASCII'>
    

搜索结果正确,但输出格式不是我想要的。所以我需要在这里重命名东西。除非有更好的方法来获取(打印)结果?目前,这通过 print( f ) 获得输出。

我唯一需要从这个输出中获取实际文件名:
FW_A01_2020-12-01_1856_C0004.txt and FW_A01_2020-12-01_1900_C0007.txt
然后我需要重命名并添加完整路径,最后将这些搜索结果文件(剪辑列表)存储在一个连续列表中,格式如下:

> '/Volumes/RAID/Data/Media/TWO_CHAIRS/footage/FW_A01_2020-12-01_1806_C0001/FW_A01_2020-12-01_1806_C0001_000000.dng', '/Volumes/RAID/Data/Media/TWO_CHAIRS/footage/FW_A01_2020-12-01_1806_C0001/FW_A01_2020-12-01_1806_C0001_000000.dng',
  1. 重命名“搜索结果”文件名(并添加完整路径),然后将它们存储在变量中。由于我(尚)不知道如何将我的实际搜索结果通过管道传输到此函数中,因此我将使用 rootDir 来执行“重命名”作为测试。

    for currentFile in listofFiles:
    listofFiles = listdir(rootDir)
    for currentFile in listofFiles:
        sourceFile = rootDir + "/" + currentFile
        mainNameEnd = currentFile.find('.')
        newFileName = currentFile[:mainNameEnd] + '_000000.dng'
        dirLoc = currentFile[:mainNameEnd]
        fullPathName = "'" + mediaDir + project.GetName() + "/" + "footage" + "/" + dirLoc + "/" + newFileName + "'" + "," + " "
        print("Converting path name: " + fullPathName)
    

输出:

Converting path name: '/Volumes/RAID/Data/Media/TWO_CHAIRS/footage/FW_A01_2020-12-01_1806_C0001/FW_A01_2020-12-01_1806_C0001_000000.dng',
Converting path name: '/Volumes/RAID/Data/Media/TWO_CHAIRS/footage/FW_A01_2020-12-01_1812_C0003/FW_A01_2020-12-01_1812_C0003_000000.dng',
Converting path name: '/Volumes/RAID/Data/Media/TWO_CHAIRS/footage/FW_A01_2020-12-01_1856_C0004/FW_A01_2020-12-01_1856_C0004_000000.dng',

太好了,正是我需要的输出格式。但是,这只适用于在 rootDir 中找到的文件。我真正需要的是获取“搜索结果”剪辑列表并以相同的方式重命名这些文件。此外,剪辑列表需要是如前所示的连续线。

一旦它起作用,我将在下面的函数中使用重新调整的剪辑列表。然后,这会将剪辑导入到外部应用中。

# Import clips from Search Result
# We insert the search_result_clip_list, separated by comma. 
clips = resolve.GetMediaStorage().AddItemsToMediaPool(search_result_clip_list)  # <-- clip list goes here 
print(search_result_clip_list)

简而言之,我不知道如何获取我的搜索结果,创建一个列表,最后在上面的函数中使用该列表。

有人知道如何正确实施吗?

python 3.6.8 | MacOS 10.13.2 |达芬奇决心 15

2 个答案:

答案 0 :(得分:0)

要获取同一列表中的所有名称:

您可以使用空列表并在每个循环中向其添加项目,如下所示:

my_names_list = []
for currentFile in listofFiles:
    sourceFile = rootDir + "/" + currentFile
    mainNameEnd = currentFile.find('.')
    newFileName = currentFile[:mainNameEnd] + '_000000.dng'
    dirLoc = currentFile[:mainNameEnd]
    fullPathName = "'" + mediaDir + project.GetName() + "/" + "footage" + "/" + dirLoc + "/" + newFileName + "'" + "," + " "
    print("Converting path name: " + fullPathName)
    my_names_list.append(fullPathName)

您将获得一个列表,其中包含所有名称作为其项目。 尊重这一点:However, this only works with files found in rootDir我真的不明白你想要什么,试着更具体。

答案 1 :(得分:0)

真实文件名在变量 files 中,您应该简单地使用

print(files)

f 中,你有文件对象,它从文件中读取数据 - 而不是文件名 - 最终你可以使用

print( f.name )

但我更喜欢第一个版本。


编辑:

如果你想保留所有匹配正则表达式的文件名,那么你应该使用列表。

循环前创建searchResult = [],循环内使用searchResult.append( files )

searchResult = []

for files in glob.glob( "*.txt" ):
    # ... code ...
    if (re.search(r'\b'+ re.escape(searchPhrase) + r'\b', contents, re.IGNORECASE)):
        print( files )
        searchResult.append( files )
相关问题