python如何將txt文件的內(nèi)容逐行讀取轉(zhuǎn)化成數(shù)組
將txt文件的內(nèi)容逐行讀取轉(zhuǎn)化成數(shù)組
例:
將train5bottle.names的每行內(nèi)容提取出來(lá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原封不動(dòng)的追加到a的末尾上,會(huì)改變a的值 #strip()用于移除字符串頭尾指定的字符(默認(rèn)為空格或者換行符)或字符序列 print(result) print(result[0]) #運(yùn)行結(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ù)組(將時(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ù)庫(kù)上進(jìn)行復(fù)用
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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python常見加密模塊用法分析【MD5,sha,crypt模塊】
這篇文章主要介紹了Python常見加密模塊用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了MD5,sha與crypt模塊加密的相關(guān)實(shí)現(xiàn)方法與操作技巧,需要的朋友可以參考下2017-05-05pytorch在fintune時(shí)將sequential中的層輸出方法,以vgg為例
今天小編就為大家分享一篇pytorch在fintune時(shí)將sequential中的層輸出方法,以vgg為例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-08-08matplotlib.pyplot畫圖 圖片的二進(jìn)制流的獲取方法
今天小編就為大家分享一篇matplotlib.pyplot畫圖 圖片的二進(jìn)制流的獲取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-05-05基于Python實(shí)現(xiàn)格斗小游戲的示例代碼
格斗游戲,曾經(jīng)是街機(jī)廳里最火爆的游戲之一,甚至可以把“之一”去掉,那個(gè)年代的格斗游戲就是街機(jī)游戲的王。本文就來(lái)用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的格斗游戲,感興趣的可以了解一下2023-03-03解鎖Python并發(fā)編程中多線程與多進(jìn)程的應(yīng)用
本文我們將先從基本概念開始,然后通過詳細(xì)舉例探討每一種機(jī)制,特別關(guān)注多線程和多進(jìn)程的應(yīng)用,最后分享一些實(shí)戰(zhàn)經(jīng)驗(yàn)以及一種優(yōu)雅的編程技巧,希望對(duì)大家有所幫助2023-05-05淺談Keras參數(shù) input_shape、input_dim和input_length用法
這篇文章主要介紹了淺談Keras參數(shù) input_shape、input_dim和input_length用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-06-06python實(shí)現(xiàn)的生成隨機(jī)迷宮算法核心代碼分享(含游戲完整代碼)
這篇文章主要介紹了python實(shí)現(xiàn)的隨機(jī)迷宮生成算法核心代碼分享,本文包含一個(gè)簡(jiǎn)單迷宮游戲完整代碼,需要的朋友可以參考下2014-07-07使用TFRecord存取多個(gè)數(shù)據(jù)案例
今天小編就為大家分享一篇使用TFRecord存取多個(gè)數(shù)據(jù)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-02-02Matplotlib實(shí)現(xiàn)subplot和subplots簡(jiǎn)單對(duì)比
在畫布創(chuàng)建子圖會(huì)有很多方法,本文主要介紹了Matplotlib實(shí)現(xiàn)subplot和subplots簡(jiǎn)單對(duì)比,簡(jiǎn)單的介紹了這兩種方法區(qū)別,感興趣的可以了解一下2021-05-05