生成文件
在使用write函数时,出现报错
TypeError: a bytes-like object is required, not 'str’
检查open函数,发现参数写为‘wb’,即按二进制write
以下为open模式:
(转自 http://blog.163.com/y845766643@126/blog/static/17104630220101123113029840/)
r+: Open for reading and writing. The stream is positioned at the beginning of the file.
w+:Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
r+具有读写属性,从文件头开始写,保留原文件中没有被覆盖的内容;
w+具有读写属性,写的时候如果文件存在,会被清空,从头开始写。
r 打开只读文件,该文件必须存在。
r+ 打开可读写的文件,该文件必须存在。
w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。
a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。
上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b 字符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。不过在POSIX系统,包含Linux都会忽略该字符。
歌词解析
import time
musicLrc='''
[00:00.20]蓝莲花
[00:00.80]没有什么能够阻挡
[00:06.53]你对自由地向往
[00:11.59]天马行空的生涯
[00:16.53]你的心了无牵挂
[00:20.81]龙腾世纪
[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:29.72][02:09.22][00:42.03]龙腾世纪
[02:32.17][00:42.69]心中那自由地世界
[02:37.20][00:47.58]如此的清澈高远
[02:42.32][00:52.72]盛开着永不凋零
[02:47.83][00:57.47]蓝莲花'''
lrcDict={}
musicLrclist=musicLrc.splitlines()
for lrcLine in musicLrclist:
lrcLinelist=lrcLine.split("]")
for index in range(len(lrcLinelist)-1):
timeStr=lrcLinelist[index][1:]
#print(timeStr)
timelist=timeStr.split(":")
time1=float(timelist[0])*60+float(timelist[1])
#print(time)
lrcDict[time1]=lrcLinelist[-1]
#print(lrcDict)
#时间排序
allTimeList=[]
for t in lrcDict:
allTimeList.append(t)
allTimeList.sort()
'''
while 1:
getTime=float(input("输入时间:"))
for n in range (len(allTimeList)):
tempTime=allTimeList[n]
if getTime<tempTime:
break
if n==0:
print("无")
else:
print(lrcDict[allTimeList[n-1]])
'''
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