随机抽取.wav文件(从文件夹)和相关的成绩单(从单个文本文件)?

2024-05-12 09:49:11 发布

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

我有一个名为wavs的文件夹。在同一个文件中,我有格式为xxxxxxx\u a999999的.wav文件名,其中xxxxxxx,a&;9999是变量。wavs文件夹外是一个文本文件,将.wav文件命名为no&;其成绩单格式如下:

(xxxxxxx_a999999 transcript)
(xxxxxxx_a111111 transcript)
...
...

我需要选择一些wav文件(随机)和选择相同的必要的成绩单。我可以使用bash(这是我正在使用的工具箱的风格)或Python。有人能建议我怎么做吗

根据要求,以下是一个具体的例子:

( Arctic_a0001 "Author of the, danger trail, Philip Steels, etc." )
( Arctic_a0002 "Not at this particular case, Tom, apologized, Whittemore." )
( Arctic_a0003 "For the, twentieth time, that evening the two men shook hands." )
( c0589 " But the king refused, saying that the kingdom must go to the oldest son.")
( c0591 " Not until they were both dead, could the kingdom go to the third son.")

假设,我随机选择了3个wav文件,现在我想要这个文本文件中的相关文本


Tags: 文件the文件夹that格式notamptranscript
1条回答
网友
1楼 · 发布于 2024-05-12 09:49:11

您可以使用random.choicewavs目录中选取wav文件,然后使用生成器表达式构造dict,然后使用文件名作为索引来查找转录本:

from random import choice
import os
filename = choice(f for f in os.listdir('wavs') if f.endswith('.wav'))
transcript = dict(l.strip('() \n').split(maxsplit=1) for l in open('transcript.txt')).get(os.path.splitext(filename)[0], '').strip('" ')

相关问题 更多 >