应用不同条件更改文件内容

2024-05-23 22:44:15 发布

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

我正在尝试对输入文件的内容做一些更改。我的输入文件如下所示:

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")     

Tags: testtxtlineitemstartenddurationln
2条回答

在位编辑文本文件相当困难。您的最佳选择是:

  1. 以Unix filter的形式编写程序,即在sys.stdout上生成新文件并使用外部工具将其放置到位

  2. 读入整个文件,然后在内存中构造新文件并将其写出。

遵循第二条思路的程序如下所示:

# read transcriptions.txt into a list of lines
with open("transcriptions.txt", "r") as tFile:
    inp = tFile.readlines()

# do processing and build a new list of lines
outp = []
for ln in inp:
    if not to_be_deleted(ln):
        outp.append(transform(ln))

# now overwrite transcriptions.txt
with open("transcriptions.txt", "w") as tFile:
    tFile.writelines(outp)

如果您将处理位写成一个列表,那就更好了:

outp = [transform(ln) for ln in inp
                      if not to_be_deleted(ln)]

以下脚本应执行您所需的操作:

import sys

def main(src, dest):
    with open(dest, 'w') as output:
        with open(src) as source:
            for line in source:
                try:
                    start, end, sound = line.split()
                except ValueError:
                    continue
                if sound == 'ay':
                    start = int(start)
                    end = int(end)
                    offset = (end - start) // 2
                    output.write('%s %s ao\n' % (start, start + offset))
                    output.write('%s %s ih\n' % (start + offset, end))
                else:
                    output.write(line)

if __name__ == "__main__":

    main(*sys.argv[1:])

输出:

18800000 20400000 pau
20400000 21300000 aa
21300000 22500000 p
22500000 23200000 l
23200000 23600000 ao
23600000 24000000 ih
24000000 25000000 k
25000000 26500000 pau

相关问题 更多 >