2011-11-21 63 views
1

我正在尝试对输入文件的内容进行一些更改。输入文件我看起来像下面这样:更改应用不同条件的文件的内容

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

一旦你得到你的代码运行,你需要测试,如果__name__ ==“__main__”,只有执行测试,如果这是真的 –

回答

1

以下脚本应该做你想做的事:

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 
+0

非常感谢... – zingy

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)] 
+1

过滤器的方法肯定是好多了。更易于调试,并且可以获得其他UNIX文本操作工具的强大功能。 –

+0

我怎样才能参考每一行的项目? – zingy

+0

@zingy:做'开始,结束,声音= ln.split()'。 –

相关问题