changing the contents of a file applying different conditions
<p>I am trying to do some changes in the contents of an input file. The input file I have looks like the following:</p>

<pre><code>18800000 20400000 pau
20400000 21300000 aa
21300000 22500000 p
22500000 23200000 l
23200000 24000000 ay
24000000 25000000 k
25000000 26500000 pau
</code></pre>

<p>This file is a transcription of an audio file. The first number denotes the start time and the next one denotes the end time. Then the alphabets denote the sound. </p>

<p>The change I have to make is, there are a few sounds which is made of two different sounds ie there are some diphthongs too. So these diphthongs have to be split into the two sounds. In the example above the diphthong is 'ay'. It is made of 'ao' and 'ih'.
What happens here is, the duration of 'ay' which is 24000000 - 232000000 = 8 is distributed into these two sounds. The result will be,</p>

<pre><code>23200000 24000000 ay
</code></pre>

<p>changes to</p>

<pre><code>23200000 236000000 ao
23600000 240000000 ih
</code></pre>

<p>I have attempted to write a pseudo code which looks rubbish.</p>

<pre><code>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")  
</code></pre>

<p>Thank you. </p>

<p>With the suggestions I was given I changed the code to the following. It is still not correct. </p>

<pre><code>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")     
</code></pre>
