此前写过这篇:正则表达式之处理一组lrc听力文件
室友近期在学Python,觉得上篇用的方法不够直观,理解起来有些费力,希望看到一个用正则找到文本之后进行替换的demo~ 作为中国好室友,我当然要给出demo啦
需求描述为:将时间格式改为以秒为单位。eg:[01:11.36]=>[71.36]
#待处理文本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
:::text
[00:00.12]section 2.
[00:02.68]you will part of a radio programme about the opening of a new local sports shop.
[00:09.32]first you have some time to look at questions 11 to 16.
[00:39.64]now listen carefully and answer questions 11 to 16.
[00:48.24]now we go to Jane who is going to tell us about what's happening in town this weekend.
[00:52.24]right,thanks Andrew,
[00:53.92]and now on to what's new,
[00:56.48]and do we really need yet another sports shop in Bradcaster?
[01:01.24]well,most of you probably know Sports World-
[01:04.44]the branch of a Danish sports goods company that opened a few years ago-
[01:09.04]it's attracted a lot of custom,
[01:11.36]and so the company has now decided to open another branch in the area.
[01:16.60]it's going to be in the shopping centre to the west of Bradcaster,
[01:20.44]so that will be good news for all of you who've found the original shop in the north of the town hard to get to.
[01:27.12]i was invited to a special preview
[01:29.60]and i can promise you,this is the ultimate in sports retailing.
......
|
#解决方案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/usr/bin/env python
# encoding: utf-8
import re
from decimal import Decimal as D
def min_sec_str2sec_str(match):
min_sec_str = match.groupdict("thetime")
#return min_sec_str
'''minute:second change to second'''
(m,s) = min_sec_str.split(":")
sec = 60*D(m)+D(s)
sec_str = str(sec)
return sec_str
def format_file_time(filename):
outputfile = open("output.lrc","w")
#命名分组
thetime = re.compile(r"(?P<thetime>\d{2}:.{5})")
with open(filename,"r") as input_file:
for line in input_file:
format_line = thetime.sub(min_sec_str2sec_str,line)
#print format_line
outputfile.write(format_line)
outputfile.close()
|