Python使用正则匹配处理文本

文本内容处理

import re
pattern = re.compile('xmin = .*\n.*xmax = .*\n.*text = ".*"')
pattern1 = re.compile('xmax = .*')
pattern2 = re.compile('xmin = .*')


# with open('./041.TextGrid') as lines:  #一次性读入txt文件,并把内容放在变量lines中
#     # m = pattern1.findall(lines)
#     print(lines)
content = open('./041.TextGrid').read() #  读取文本内容  为字符串str
m = pattern.findall(content) # 根据正则匹配所有合适的内容放入list
lens = len(m)  # 列表长度
# print(type(m)) # 查看类型

for i in m:
    temp1 = i.split('\n')[0].strip().split('=')[1] #循环后的值进行切分 list下标为  0,1,2
    temp2 = i.split('\n')[1].strip().split('=')[1] 
    temp3 = i.split('\n')[2].strip().split('=')[1] 
    # 根据内容剔除不需要的内容
    if temp3 == ' "sp"':
        continue
    elif temp3 == ' "sil"':
        pass
    elif temp3 == ' "d"':
        pass
    else:
        print('xmin:{}; xmax:{}; text:{}'.format(temp1, temp2, temp3))
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值