如何用python將srt文件轉化成數組形式

原srt文件

0
00:00:00,150 --> 00:00:11,430
Fighting this pandemic needs political commitment and commitment at the highest level possible and the President's commitment.

1
00:00:11,431 --> 00:00:16,020
you have what it is in it and the would it have seen it.

2
00:00:16,021 --> 00:00:19,320
and that kind of leadership is very,

3
00:00:19,321 --> 00:00:20,160
very important.

4
00:00:20,161 --> 00:00:21,570
The whole of government approach.

轉化之後的數組(將時間和內容分離)

['00:00', '00:11', '00:16', '00:19', '00:20']

["Fighting this pandemic needs political commitment and commitment at the highest level possible and the President's commitment.", 'you have what it is in it and the would it have seen it.', 'and that kind of leadership is very,', 'very important.', 'The whole of government approach.']

下面貼出轉化的代碼,即將cte_test.srt轉化成數組
之後可以考慮輸送到mysql數據庫上進行復用

count1 = 1
count2 = 2
ktime = []
klrc = []

with open('cte_test.srt', 'r') as f:
    for index, value in enumerate(f.readlines()):
        if index==count1:
            value= value.strip()[3:8]
            ktime.append(value)
            count1=count1+4
        elif index==count2:
            value= value.strip()
            klrc.append(value)
            count2=count2+4

print(ktime)
print(klrc)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章