textgrid python open read spliten和write to file

时间:2018-04-14 21:19:18

标签: python split

我有以下textgrid:

 File type = "ooTextFile"
 Object class = "TextGrid"

 xmin = 0 
 xmax = 3931.56874994773
 tiers? <exists> 
 size = 4
item []:
  item [1]:
    class = "IntervalTier"
    name = "Phrases"
    xmin = 0
    xmax = 3931.56874994773
    intervals: size = 1938
    intervals [1]:
        xmin = 0
        xmax = 3.59246613841739
        text = "Good morning"
    intervals [2]:
        xmin = 3.59246613841739
       .
       .
     item [2]:
     class = "IntervalTier"
     name = "Phrases_2"
     xmin = 0

如何在4个文件中分割这个文本(项目[1],项目[2],项目[3],项目[4])(每个文件的名称是项目中的名称){{ 1}}例如Item [1]是Phrases.textgrid,而item [2]是Phrases_2.textgrid等...

1 个答案:

答案 0 :(得分:0)

读取文件:陌生人 - 代码:

import os
os.chdir('/python')
# c:\python is my work folder 
import re
with open('1.Textgrid','r') as f:
# 1.textgrid is the file to read and split
data = f.read()
#print data #Use this to view how 
#the code would look like after the       program has opened the files
txttext = ''
#informations needed begin on the 9th lines
for lines in data[9:]:  
lines = re.sub('\n','',lines)

#as there's \n at the end of every sentence.
lines = re.sub ('^ *','',lines)
#To remove any special characters
linepair = lines.split('=')
if len(linepair) == 2:
if linepair[0] == 'xmin':
   xmin == linepair[1]
if linepair[0] == 'xmax':
   xmax == linepair[1]
if linepair[0] == 'text':
   if linepair[1].strip().startswith('"') 
and linepair[1].strip().endswith('"'):
     text = linepair[1].strip()[1:-1]
     txttext += text + '\n' 

如何打破每个项目中的文本并将其写入新文件