具体思路
1、提取歌词的文本文件
2、将文件中的时间点提取出来,并将歌词分成多个字符串保存下来——构成字典,其中时间为关键字key、歌词为value。
3、形成字典后就可以利用其中的关系进行运用
可以设计一个简单的歌词打印程序
import time
musicLrc = open("文件路径", "r")
lrcDict = {}
musicLrcList = musicLrc.splitlines()
for lrcLine in musicLrcList:
lrcLineList = lrcLine.split("]")
for index in range(len(lrcLineList) - 1):
timeStr = lrcLineList[index][1:]
timeList = timeStr.split(":")
time1 = float(timeList[0]) * 60 + float(timeList[1])
lrcDict[time1] = lrcLineList[-1]
allTimeList = []
for t in lrcDict:
allTimeList.append(t)
allTimeList.sort()
getTime = 0
while 1:
for n in range(len(allTimeList)):
tempTime = allTimeList[n]
if getTime < tempTime:
break
lrc = lrcDict.get(allTimeList[n - 1])
if lrc == None:
pass
else:
print(lrc)
time.sleep(1)
getTime += 1