我正在尝试对输入文件的内容做一些更改。我的输入文件如下所示:
18800000 20400000 pau
20400000 21300000 aa
21300000 22500000 p
22500000 23200000 l
23200000 24000000 ay
24000000 25000000 k
25000000 26500000 pau
此文件是音频文件的转录。第一个数字表示开始时间,下一个数字表示结束时间。然后字母表表示声音。你知道吗
我要做的改变是,有几个音是由两个不同的音组成的(也有一些双元音)。所以这些双元音必须分成两个音。在上面的例子中,双元音是“ay”。它由“ao”和“ih”组成。 这里发生的是,“ay”的持续时间24000000-232000000=8被分配到这两个声音中。结果是
23200000 24000000 ay
更改为
23200000 236000000 ao
23600000 240000000 ih
我试图写一个看起来垃圾的伪代码。你知道吗
def test(transcriptionFile) :
with open("transcriptions.txt", "r+") as tFile :
for line in tFile :
if 3rd_item = ay
duration = (2nd_item[1] - 1st_item[2]) / 2
delete the line
tFile.write(1st_item, 1st_item + d, ao)
tfile.write(1st_item + d, 1st_item, ih) # next line
if__name__ == "__main__" :
test("transcriptions.txt")
谢谢你。你知道吗
根据别人给我的建议,我把代码改成了下面的代码。这仍然是不正确的。你知道吗
def test(transcriptionFile) :
with open("transcriptions.txt", "r") as tFile :
inp = tFile.readlines()
outp = []
for ln in inp :
start, end, sound = ln.strip()
if sound == ay :
duration = (end - start) / 2
ln.delete
start = start
end = start + duration
sound = ao
outp.append(ln)
start = start + duration # next line
end = start
sound = ih
outp.append(ln)
with open("transcriptions.txt", "w") as tFile:
tFile.writelines(outp)
__name__ == "__main__"
test("transcriptions.txt")
在位编辑文本文件相当困难。您的最佳选择是:
以Unix filter的形式编写程序,即在
sys.stdout
上生成新文件并使用外部工具将其放置到位读入整个文件,然后在内存中构造新文件并将其写出。
遵循第二条思路的程序如下所示:
如果您将处理位写成一个列表,那就更好了:
以下脚本应执行您所需的操作:
输出:
相关问题 更多 >
编程相关推荐