python中输出文本的指定行
1、输出文本的前三行
[root@PC1 test2]# ls a.txt test.py [root@PC1 test2]# cat a.txt ## 测试数据 1 u j d 2 s f f 3 z c e 4 z v k 5 c f y 6 w q r 7 a d g 8 e f s [root@PC1 test2]# cat test.py ## 程序 #!/usr/bin/python in_file = open("a.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() for i in range(0, 3): out_file.write(lines[i]) in_file.close() out_file.close() [root@PC1 test2]# python test.py ##执行程序 [root@PC1 test2]# ls a.txt result.txt test.py [root@PC1 test2]# cat result.txt ##结果 1 u j d 2 s f f 3 z c e
2、输出文本的最后5行
001、
[root@PC1 test2]# ls a.txt test.py [root@PC1 test2]# cat a.txt 1 u j d 2 s f f 3 z c e 4 z v k 5 c f y 6 w q r 7 a d g 8 e f s [root@PC1 test2]# cat test.py #!/usr/bin/python in_file = open("a.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() lines = lines[-5:] for i in lines: out_file.write(i) in_file.close() out_file.close() [root@PC1 test2]# python test.py [root@PC1 test2]# ls a.txt result.txt test.py [root@PC1 test2]# cat result.txt 4 z v k 5 c f y 6 w q r 7 a d g 8 e f s
002、
[root@PC1 test2]# ls a.txt test.py [root@PC1 test2]# cat a.txt 1 u j d 2 s f f 3 z c e 4 z v k 5 c f y 6 w q r 7 a d g 8 e f s [root@PC1 test2]# cat test.py #!/usr/bin/python in_file = open("a.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() for i in range(len(lines) - 5, len(lines)): out_file.write(lines[i]) in_file.close() out_file.close() [root@PC1 test2]# python test.py [root@PC1 test2]# ls a.txt result.txt test.py [root@PC1 test2]# cat result.txt 4 z v k 5 c f y 6 w q r 7 a d g 8 e f s
3、提取偶数行或奇数行
[root@PC1 test2]# ls a.txt test.py [root@PC1 test2]# cat a.txt 1 u j d 2 s f f 3 z c e 4 z v k 5 c f y 6 w q r 7 a d g 8 e f s [root@PC1 test2]# cat test.py ## 程序 #!/usr/bin/python in_file = open("a.txt", "r") out_file1 = open("odd.txt", "w") out_file2 = open("even.txt", "w") lines = in_file.readlines() for i in range(0, len(lines)): if i % 2 == 0: out_file1.write(lines[i]) else: out_file2.write(lines[i]) in_file.close() out_file1.close() out_file2.close() [root@PC1 test2]# python test.py [root@PC1 test2]# ls a.txt even.txt odd.txt test.py [root@PC1 test2]# cat odd.txt 1 u j d 3 z c e 5 c f y 7 a d g [root@PC1 test2]# cat even.txt 2 s f f 4 z v k 6 w q r 8 e f s
4、提取不定行
[root@PC1 test2]# ls a.txt test.py [root@PC1 test2]# cat a.txt 1 u j d 2 s f f 3 z c e 4 z v k 5 c f y 6 w q r 7 a d g 8 e f s [root@PC1 test2]# cat test.py ## 提取2、3、5、7行 #!/usr/bin/python in_file = open("a.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() num = [2, 3, 5, 7] for i in num: out_file.write(lines[i - 1]) in_file.close() out_file.close() [root@PC1 test2]# python test.py [root@PC1 test2]# ls a.txt result.txt test.py [root@PC1 test2]# cat result.txt ## 查看结果 2 s f f 3 z c e 5 c f y 7 a d g
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
2021-06-01 c语言 11-8
2021-06-01 c语言 11-7
2021-06-01 c语言中转换字符串函数 atoi函数
2021-06-01 c语言中 strncmp函数, 函数原型和头文件。
2021-06-01 c语言中strcmp函数,函数原型和函数头文件
2021-06-01 c语言中strncat函数,函数原型以头文件
2021-06-01 c语言中strcat函数,函数原型和函数头文件