{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "1c256e43", "metadata": {}, "outputs": [], "source": [ "a = 1\n", "b = 2\n", "c = 3\n", "state = a\n", "text = ''\n", "with open('test.srt', 'r') as f: #打开srt字幕文件,并去掉文件开头的\\ufeff\n", " for line in f.readlines(): #遍历srt字幕文件\n", " if state == a: #跳过第一行\n", " state = b\n", " elif state == b: #跳过第二行\n", " state = c\n", " elif state == c: #读取第三行字幕文本\n", " if len(line.strip()) !=0:\n", " text += ' ' + line.strip() #将同一时间段的字幕文本拼接\n", " state = c\n", " elif len(line.strip()) ==0:\n", " with open('test1.txt', 'a') as fa: #写入txt文本文件中\n", " fa.write(text)\n", " text = '\\n'\n", " state = a" ] }, { "cell_type": "code", "execution_count": null, "id": "fcc101e2", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }