class Lyric:
def __init__(self, time, word):
self.time = time
self.word = word
def __str__(self):
return '%.2f%s' % (self.time, self.word)
def __gt__(self, other):
return self.time > other.time
class LyricAnalysis:
def __init__(self, son_name):
self.son_name = son_name
self.all_lyric = []
self.get_Lyric()
def get_time_word(self,content):
contents = content.split(']')
word=contents[-1][:-1]
for time in contents[:-1]:
times=time[1:].split(':')
fen=float(times[0])*60
miao=float(times[1])
new_time=fen+miao
lyric=Lyric(new_time,word)
self.all_lyric.append(lyric)
def get_Lyric(self):
with open('./%s'%(self.son_name),'r',encoding='utf-8')as ff:
content=ff.readline()
while content:
self.get_time_word(content)
self.all_lyric.sort(reverse=True)
content=ff.readline()
for lyric in self.all_lyric:
print(lyric)
def get_word(self,time):
for lyric in self.all_lyric:
if time > lyric.time:
return lyric.word
ni=LyricAnalysis('蓝莲花.txt')
print(ni.get_word(53))
输出:
167.83蓝莲花
162.32盛开着永不凋零
157.20如此的清澈高远
152.17心中那自由地世界
146.79才发觉脚下的路
141.81当你低头地瞬间
136.51也曾感到彷徨
131.27穿过幽暗地岁月
125.72才发觉脚下的路
120.60当你低头地瞬间
115.46也曾感到彷徨
110.22穿过幽暗地岁月
57.47蓝莲花
52.72盛开着永不凋零
47.58如此的清澈高远
42.69心中那自由地世界
37.16才发觉脚下的路
32.30当你低头地瞬间
26.83也曾感到彷徨
21.95穿过幽暗地岁月
16.53你的心了无牵挂
11.59天马行空的生涯
6.53你对自由地向往
0.80没有什么能够阻挡
0.20蓝莲花
盛开着永不凋零
Process finished with exit code 0