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

 更新时间:2023年03月25日 16:20:19   作者:初语之然  
这篇文章主要介绍了python如何将txt文件的内容逐行读取转化成数组问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

将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)

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python通过实例讲解反射机制

    python通过实例讲解反射机制

    这篇文章主要介绍了python通过实例讲解反射机制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Python定时任务随机时间执行的实现方法

    Python定时任务随机时间执行的实现方法

    这篇文章主要介绍了Python定时任务随机时间执行的实现方法,文中给大家提到了python定时执行任务的三种方式 ,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • 详解如何将Pandas运行结果导出为CSV格式文件

    详解如何将Pandas运行结果导出为CSV格式文件

    在数据分析与处理的世界里,Pandas 是一个不可或缺的工具,它提供了强大的数据处理功能,让我们能够轻松地清洗、转换和分析数据,本文将带领大家了解如何使用 Pandas 将运行结果导出为 CSV 文件,并通过实际案例进行深入探讨
    2024-05-05
  • pycharm远程连接服务器运行pytorch的过程详解

    pycharm远程连接服务器运行pytorch的过程详解

    这篇文章主要介绍了在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorch的过程,包括安装PyTorch、CUDA以及配置PyCharm远程开发环境的详细步骤,需要的朋友可以参考下
    2025-02-02
  • 用 Python 爬了爬自己的微信朋友(实例讲解)

    用 Python 爬了爬自己的微信朋友(实例讲解)

    下面小编就为大家带来一篇用 Python 爬了爬自己的微信朋友(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • 如何在Python中实现goto语句的方法

    如何在Python中实现goto语句的方法

    这篇文章主要介绍了如何在Python中实现goto语句的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • 详细介绍Python进度条tqdm的使用

    详细介绍Python进度条tqdm的使用

    这篇文章主要介绍了详细介绍Python进度条tqdm的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • python中的socket实现ftp客户端和服务器收发文件及md5加密文件

    python中的socket实现ftp客户端和服务器收发文件及md5加密文件

    这篇文章主要介绍了python中的socket实现ftp客户端和服务器收发文件及md5加密文件的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • Python守护进程用法实例分析

    Python守护进程用法实例分析

    这篇文章主要介绍了Python守护进程用法,实例分析了Python守护进程的功能及使用方法,需要的朋友可以参考下
    2015-06-06
  • python使用turtle绘制国际象棋棋盘

    python使用turtle绘制国际象棋棋盘

    这篇文章主要为大家详细介绍了python使用turtle画国际象棋棋盘,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05

最新评论