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原封不動(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定時(shí)任務(wù)隨機(jī)時(shí)間執(zhí)行的實(shí)現(xiàn)方法
這篇文章主要介紹了Python定時(shí)任務(wù)隨機(jī)時(shí)間執(zhí)行的實(shí)現(xiàn)方法,文中給大家提到了python定時(shí)執(zhí)行任務(wù)的三種方式 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
詳解如何將Pandas運(yùn)行結(jié)果導(dǎo)出為CSV格式文件
在數(shù)據(jù)分析與處理的世界里,Pandas?是一個(gè)不可或缺的工具,它提供了強(qiáng)大的數(shù)據(jù)處理功能,讓我們能夠輕松地清洗、轉(zhuǎn)換和分析數(shù)據(jù),本文將帶領(lǐng)大家了解如何使用?Pandas?將運(yùn)行結(jié)果導(dǎo)出為?CSV?文件,并通過實(shí)際案例進(jìn)行深入探討2024-05-05
pycharm遠(yuǎn)程連接服務(wù)器運(yùn)行pytorch的過程詳解
這篇文章主要介紹了在Linux環(huán)境下使用Anaconda管理不同版本的Python環(huán)境,并通過PyCharm遠(yuǎn)程連接服務(wù)器來運(yùn)行PyTorch的過程,包括安裝PyTorch、CUDA以及配置PyCharm遠(yuǎn)程開發(fā)環(huán)境的詳細(xì)步驟,需要的朋友可以參考下2025-02-02
用 Python 爬了爬自己的微信朋友(實(shí)例講解)
下面小編就為大家?guī)硪黄?Python 爬了爬自己的微信朋友(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
如何在Python中實(shí)現(xiàn)goto語(yǔ)句的方法
這篇文章主要介紹了如何在Python中實(shí)現(xiàn)goto語(yǔ)句的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件
這篇文章主要介紹了python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python守護(hù)進(jìn)程用法實(shí)例分析
這篇文章主要介紹了Python守護(hù)進(jìn)程用法,實(shí)例分析了Python守護(hù)進(jìn)程的功能及使用方法,需要的朋友可以參考下2015-06-06

