合并中英字幕的python小脚本
文件读取写入,正则,没啥含量。
#!e:/python32/python.exe
import os
import re
pat = r'(.*)\.chs\.srt'
for f in os.listdir():
m = re.match(pat, f)
if not m:
continue
ff = m.group(1)
fop = open(ff + '.all.srt', 'w')
linesChs = open(ff + '.chs.srt').readlines()
linesEng = open(ff + '.eng.srt').readlines()
iChs = 0
iEng = 2
lenChs = len(linesChs)
flag = True
while True:
if iChs >= lenChs:
break
if flag:
if linesChs[iChs].strip() == '':
flag = False
iChs += 1
continue
fop.write(linesChs[iChs])
iChs += 1
continue
if linesEng[iEng].strip() == '':
fop.write('\n')
flag = True
iEng += 3
continue
fop.write(linesEng[iEng])
iEng += 1
fop.close()