python如何將txt文件的內(nèi)容逐行讀取轉(zhuǎn)化成數(shù)組
將txt文件的內(nèi)容逐行讀取轉(zhuǎn)化成數(shù)組
例:
將train5bottle.names的每行內(nèi)容提取出來轉(zhuǎn)化成數(shù)組
轉(zhuǎn)換代碼:
result = [] with open(r'E:\HISI\darknet-master\build\darknet\x64\data\train5bottle.names' ,'r') as f: for line in f: result.append(line.strip().split(',')[0]) #a.append(b):是將b原封不動的追加到a的末尾上,會改變a的值 #strip()用于移除字符串頭尾指定的字符(默認為空格或者換行符)或字符序列 print(result) print(result[0]) #運行結(jié)果: ['0degree', '6degree', '12degree', '18degree', '24degree', '30degree', '36degree', '42degree', '48degree', '54degree', '60degree', '66degree', '72degree', '78degree', '84degree', '90degree', '96degree', '102degree', '108degree', '114degree', '120degree', '126degree', '132degree', '138degree', '144degree', '150degree', '156degree', '162degree', '168degree', '174degree', '180degree'] 0degree
將srt文件轉(zhuǎn)化成數(shù)組形式
原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.
轉(zhuǎn)化之后的數(shù)組(將時間和內(nèi)容分離)
['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.']
下面貼出轉(zhuǎn)化的代碼,即將cte_test.srt轉(zhuǎn)化成數(shù)組
之后可以考慮輸送到mysql數(shù)據(jù)庫上進行復用
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)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python常見加密模塊用法分析【MD5,sha,crypt模塊】
這篇文章主要介紹了Python常見加密模塊用法,結(jié)合實例形式較為詳細的分析了MD5,sha與crypt模塊加密的相關(guān)實現(xiàn)方法與操作技巧,需要的朋友可以參考下2017-05-05pytorch在fintune時將sequential中的層輸出方法,以vgg為例
今天小編就為大家分享一篇pytorch在fintune時將sequential中的層輸出方法,以vgg為例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08matplotlib.pyplot畫圖 圖片的二進制流的獲取方法
今天小編就為大家分享一篇matplotlib.pyplot畫圖 圖片的二進制流的獲取方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05淺談Keras參數(shù) input_shape、input_dim和input_length用法
這篇文章主要介紹了淺談Keras參數(shù) input_shape、input_dim和input_length用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python實現(xiàn)的生成隨機迷宮算法核心代碼分享(含游戲完整代碼)
這篇文章主要介紹了python實現(xiàn)的隨機迷宮生成算法核心代碼分享,本文包含一個簡單迷宮游戲完整代碼,需要的朋友可以參考下2014-07-07Matplotlib實現(xiàn)subplot和subplots簡單對比
在畫布創(chuàng)建子圖會有很多方法,本文主要介紹了Matplotlib實現(xiàn)subplot和subplots簡單對比,簡單的介紹了這兩種方法區(qū)別,感興趣的可以了解一下2021-05-05