textgrid python打开读拆分并写入fi

2024-09-19 20:26:30 发布

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

我有以下文本网格:

 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个文件中用4个项目(项目[1]、项目[2]、项目[3]、项目[4])拆分文本(每个文件的名称是项目中的名称)enter code here例如,项目[1]是短语.textgrid第[2]项是短语“2.textgrid”等。。。在


Tags: 文件项目name文本名称网格sizeitem
1条回答
网友
1楼 · 发布于 2024-09-19 20:26:30

文件-陌生人阅读代码:

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' 

如何将每个项目中的文本分开并写入新文件

相关问题 更多 >