我有一个电影的字幕文件(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/