defsrt2lis(name): lines = open('srt/'+name, 'r', encoding='utf-8').readlines() lis = [] tmp = [] for i, line in enumerate(lines): # 以\n 结尾的一个时间点 if line == '\n': lis.append(tmp) tmp = [] else: tmp.append(line) return lis
defhebing(name_zh): name_en = name_zh[:-7] + '.srt' out_name='out/'+name_en f=open(out_name,'w',encoding='utf-8') lis_en=srt2lis(name_en) lis_zh=srt2lis(name_zh) for i,item in enumerate(lis_en): print(item[0]) f.write(item[0]) f.write(item[1]) for line in lis_zh[i][2:]: f.write(line) for line in item[2:]: f.write(line) f.write('\n') f.close()
flis=os.listdir('srt/') ifnot os.path.exists('out'): os.mkdir('out') for name in flis: print(name) if name[-6:-4]=='zh': hebing(name)