广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python怎么将txt文件的内容逐行读取转化成数组
  • 678
分享到

python怎么将txt文件的内容逐行读取转化成数组

2023-07-05 16:07:50 678人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

本篇内容主要讲解“python怎么将txt文件的内容逐行读取转化成数组”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python怎么将txt文件的内容逐行读取转化成数组”吧!将txt文件的内容逐

本篇内容主要讲解“python怎么将txt文件的内容逐行读取转化成数组”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习Python怎么将txt文件的内容逐行读取转化成数组”吧!

将txt文件的内容逐行读取转化成数组

例:

将train5bottle.names的每行内容提取出来转化成数组

python怎么将txt文件的内容逐行读取转化成数组

转换代码:

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 = 1count2 = 2ktime = []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+4print(ktime)print(klrc)

到此,相信大家对“python怎么将txt文件的内容逐行读取转化成数组”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: python怎么将txt文件的内容逐行读取转化成数组

本文链接: https://www.lsjlt.com/news/353413.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python怎么将txt文件的内容逐行读取转化成数组
    本篇内容主要讲解“python怎么将txt文件的内容逐行读取转化成数组”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“python怎么将txt文件的内容逐行读取转化成数组”吧!将txt文件的内容逐...
    99+
    2023-07-05
  • php怎么读取txt文件并将其转为数组
    PHP是非常灵活的编程语言,它可以读取和处理各种不同类型的文件。当我们需要读取一个文本文件并将其转换为数组时,使用PHP是非常方便的。在本文中,我们将讨论如何使用PHP读取文本文件并将其转换为数组。我们将从以下几个方面入手:打开文本文件读取...
    99+
    2023-05-14
    php php文件目录
  • 怎么在Pytorch模型中将读取的pth文件参数转换成numpy矩阵
    怎么在Pytorch模型中将读取的pth文件参数转换成numpy矩阵?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Pytorch给了很方便的读取参数接口:nn....
    99+
    2023-06-06
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作