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

时间:2019-03-14 07:19:06

标签: 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或其他任何编程语言都能达到我们这个目标吗?

1 个答案:

答案 0 :(得分:2)

您可以检查line是否以digit开头,并且只有在没有的情况下才打印:

list.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