python - 从文本中删除时间线(继续没有时间线的文本)

标签 python

我有一个电影的字幕文件(sub.srt 或文本文件)。但我想知道是否有一种简短的方法可以删除文件的所有行号和时间线。例如

85
00:07:39,250 --> 00:07:41,469
We got to be smart.
We're a ways from being finished.

86
00:07:41,628 --> 00:07:43,380
I can do this all week.

87
00:07:43,546 --> 00:07:44,547
We're gonna.

 88
00:07:44,714 --> 00:07:49,352
We're like the Comanches,
little brother, raiding wherever we please

结果必须是

We got to be smart.
We're a ways from being finished.

I can do this all week.

We're gonna.

We're like the Comanches,
little brother, raiding wherever we please

或连续形状:

We got to be smart. We're a ways from being finished. I can do this all week.  We're gonna.  We're like the Comanches, little brother, raiding wherever we please

Python 或任何其他编程语言可以帮助我们实现这一目标吗?

最佳答案

您可以检查是否以数字开头,如果不是则打印:

列表.txt:

85
00:07:39,250 --> 00:07:41,469
We got to be smart.
We're a ways from being finished.

86
00:07:41,628 --> 00:07:43,380
I can do this all week.

87
00:07:43,546 --> 00:07:44,547
We're gonna.

 88
00:07:44,714 --> 00:07:49,352
We're like the Comanches,
little brother, raiding wherever we please

因此:

with open("list.txt", 'r') as fp:
content = fp.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
    if not line[0].isdigit():
        print(line)

输出:

We got to be smart.
We're a ways from being finished.
I can do this all week.
We're gonna.
We're like the Comanches,
little brother, raiding wherever we please

编辑:

使用 print(line, end = "") 获取单行输出:

输出:

We got to be smart. We're a ways from being finished. I can do this all week. We're gonna. We're like the Comanches, little brother, raiding wherever we please

关于python - 从文本中删除时间线(继续没有时间线的文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55156903/

相关文章:

python - Jupyter Notebook 只有 Python [conda root] 和 Python [default] 内核

Python3 迭代器与生成器

python - 将多行字符串作为参数传递给 Windows 中的脚本

python - Twisted 和命令行界面

python - 是什么原因导致此Genshi的模板语法错误?

python - 如何在 Scapy 中抑制 "Sent $X packet"输出?

python - 如何获取所有 channel ID - youtube api v3

python - MySQL 选择 : Find all languages by Unicode

python - 测试创建的返回 None 的函数图

python - 类型错误 : argument of type 'NoneType' is not iterable