#想学英语,下了英语字幕,发现有很多时间戳,打印下来会很占空间,于是用python将时间戳去掉。
def model1():
f1=open('coherence.txt','r')#将下载的字幕复制(或者更名)为coherence.txt(只要是txt文件即可)并放在python根目录下
f2=open('test2.txt','w')#打开(没有则新建)test2文件
a=[':',',','>','1','2','3','4','5','6','7','8','9','0','-']
for line in f1:
for s in line:
if str(s) in a:
line=line.replace(s,'')#改为空号
f2.write(line)
f1.close()
f2.close()
def model2():
f2=open('test2.txt','r')
f3=open('test3.txt','w')
for line in f2:
if line.split():
f3.write(line)
f3.close()
model1()
model2()
效果如下:
原本字幕:
![python对字幕的改动 - Mr.Cat - Mr.Cats blog](http://img0.ph.126.net/Anq41f8qnmQz0yEttjKSiA==/6632366288934597942.png)
修改后的字幕:
![python对字幕的改动 - Mr.Cat - Mr.Cats blog](http://img2.ph.126.net/WDdfkKEZeo3SZ9XJL3rCmg==/6632346497725296650.png)