我有一个带时间的多行文本,采用 MM:SS 格式,带有视频中的字幕行。我想将 MM:SS 格式转换为 ass 格式,即 00:MM:SS,000 并使用间隔制表符输出。我写了这段代码
text = """02:42 02:47 And so that Wayne Gretzky method for sort of going into the future and
02:47 02:51 imagining what that future might look like, again, is a good idea for research."""
for line in text.splitlines():
words_in_line = line.split('\t')
for word in words_in_line:
if ":" in word:
ass= "00:"+word +",000"
final_line = line.replace(word,ass)
print(final_line)
它转换格式,但它只转换每一行中的一个时间,然后在单独的行上转换另一个,给出这样的输出
00:02:42,000 02:47 And so that Wayne Gretzky method for sort of going into the future and
02:42 00:02:47,000 And so that Wayne Gretzky method for sort of going into the future and
00:02:47,000 02:51 imagining what that future might look like, again, is a good idea for research.
02:47 00:02:51,000 imagining what that future might look like, again, is a good idea for research.
如何更改代码以获得这样的输出?
00:02:42,000 00:02:47,000 And so that Wayne Gretzky method for sort of going into the future and
00:02:47,000 00:02:51,000 imagining what that future might look like, again, is a good idea for research.