目录
将txt文件的内容逐行读取转化成数组将srt文件转化成数组形式总结将txt文件的内容逐行读取转化成数组
例:
将train5bottle.names的每行内容提取出来转化成数组
(资料图片)
转换代码:
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]) #运行结果: ["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文件转化成数组形式
原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)
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
关键词:
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?