python - 如何从字幕中提取文本?(在python中)
问题描述
我要转换这个:
1
00:00:01,710 --> 00:00:03,830
Now react came out in 2013.
2
00:00:03,840 --> 00:00:07,890
But what do we have before then before we act.
3
00:00:07,890 --> 00:00:15,040
Well the front fronting landscape was very different initially back in the 90s and early 2000s.
像这样:
thisdict = {
"1": "Now react came out in 2013.",
"1time": '00:00:01,710 --> 00:00:03,830'
}
谁能帮忙?
解决方案
你的意思是这样的吗?
with open('subtitle.srt') as file:
subtitle = file.readlines()
sub_list = [subtitle[i : i+4] for i in range(0, len(subtitle), 4)]
this_dict = {}
for item in sub_list:
number = item[0].strip('\n')
this_dict[number] = item[2].strip('\n')
this_dict[f"{number}time"] = item[1].strip('\n')
print(this_dict)
输出 :
{'1': 'Now react came out in 2013.', '1time': '00:00:01,710 --> 00:00:03,830', '2': 'But what do we have before then before we act.', '2time': '00:00:03,840 --> 00:00:07,890', '3': 'Well the front fronting landscape was very different initially back in the 90s and early 2000s.', '3time': '00:00:07,890 --> 00:00:15,040'}
推荐阅读
- ionic-framework - 如何在带有 higcharts 的 ionic 中添加绘图功能?
- android - 如何调试“java.lang.NoClassDefFoundError”?
- c# - 如何使类泛型以在 C# 中传递动态属性类
- angular - 过滤所选数据
- android - 从第二个活动向活动添加片段
- python - 两个时间之间有多少小时?(不支持的操作数类型 -:'datetime.time' 和 'datetime.time')
- php - 如何将数组值从一个数组移动到另一个数组
- android - 无法在 Android 上接收 Pushy 推送通知
- php - 使链接在 30 分钟后或使用后无效
- angular6 - Angular 6 文件上传与其他表单数据