将文本文件中的每三行替换为另一个文本文件中的内容



尝试用实际字幕替换每三行。

背景:我正在Videosubfinder和ocr api的帮助下从音乐视频和电影中制作字幕。

emptySub.srt(使用Videosubfinder自动创建(

1
00:00:10,076 --> 00:00:15,080
sub duration: 5,004
2
00:00:57,891 --> 00:01:01,694
sub duration: 3,803

字幕.txt看起来像这样(使用OCR API并循环浏览图像,您无需查看代码(

I bought some eggs.
He bought some spam.

法典

with open("empty.srt", "a") as file:
for line in file:
# TODO

预期产出

1
00:00:10,076 --> 00:00:15,080
I bought some eggs.
2
00:00:57,891 --> 00:01:01,694
He bought some spam.

我被困住了。如何用我的字幕替换?也许我应该使用我不知道的正则表达式。

编辑:我终于自己解决了

你想要一些变体:

subtitleLines = open('subtitle.txt', 'r')
# Creates a list of lines
srtLines = open('srtfile.srt', 'r').readlines()
for (i, line) in enumerate(subtitleLines):
srtLines[3*i + 2] = line
# emit srtLines

这对于 KB 到 ~MBish 范围内的文件表现良好,但如果文件很大,您将希望将 srtfile比字幕file更快地前进。如何推进打开的文件?通过呼叫next()

# after reading in subtitle.txt into subtitleFile:
for line in subtitleFile:
for i in range(2):
# Your "next" line will have a newline, so suppress print()'s
# default newline.
print(srtFile.next(), end="")
# advance without printing
srtFile.next()
print line

您将需要捕获StopIteration并决定在SRT文件"用完"后该怎么做 - 这取决于您是否要验证。

但是请注意,从您的示例中,从第 3 行开始,看起来每 4 行都是字幕行(srt 块之间有一个空行(。

subList = []
with open("subtitle.txt", "r") as subFile:
for subLine in subFile:
subList.append(subLine.rstrip())
print(subList)
i = 0
with open("emptySub.srt", "r") as file:
for line in file:
if line.startswith('s'):
line = line.replace(line, subList[i]+'n')
i = i + 1
with open('newFile.srt','a') as resFile:
resFile.write(line)

最新更新