-
(尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
[00:00.20]蓝莲花 [00:00.80]没有什么能够阻挡 [00:06.53]你对自由地向往 [00:11.59]天马行空的生涯 [00:16.53]你的心了无牵挂 [02:11.27][01:50.22][00:21.95]穿过幽暗地岁月 [02:16.51][01:55.46][00:26.83]也曾感到彷徨 [02:21.81][02:00.60][00:32.30]当你低头地瞬间 [02:26.79][02:05.72][00:37.16]才发觉脚下的路 [02:32.17][00:42.69]心中那自由地世界 [02:37.20][00:47.58]如此的清澈高远 [02:42.32][00:52.72]盛开着永不凋零 [02:47.83][00:57.47]蓝莲花
import time
import re
class Lyric:
def __init__(self):
self.lyric = {}
def load_lyric(self, filename: str):
with open(file=filename, mode='r') as f:
for each_line in f:
time_axis = re.findall('\[([0-9 | : | .]+)\]', each_line)
for each in time_axis:
time = each.split(':')
time = float(time[0]) * 60 + float(time[1])
word = re.match('.*\](.*)', each_line).group(1)
self.lyric[time] = word
def play(self):
interval = 0
for key, val in self.lyric.items():
interval = key - interval
time.sleep(interval)
print(val)
interval = key