python 中提取以指定字符开头、结尾的数据
1、测试数据
root@PC1:/home/test2# cat a.txt
d u s
x e j
z c e
e f a
z x e
w f e
2、提取以z开头的数据
root@PC1:/home/test2# ls a.txt test.py root@PC1:/home/test2# cat a.txt d u s x e j z c e e f a z x e w f e root@PC1:/home/test2# cat test.py ## 提取以z开头的数据 #!/usr/bin/python in_file = open("a.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() for i in lines: if i.startswith("z"): out_file.write(i) in_file.close() out_file.close() root@PC1:/home/test2# python test.py root@PC1:/home/test2# ls a.txt result.txt test.py root@PC1:/home/test2# cat result.txt ## 查看结果 z c e z x e
3、同时提取以z和x开头的数据
root@PC1:/home/test2# ls a.txt test.py root@PC1:/home/test2# cat a.txt d u s x e j z c e e f a z x e w f e root@PC1:/home/test2# cat test.py ## 提取以z开头或者以x开头的数据 #!/usr/bin/python in_file = open("a.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() for i in lines: if (i.startswith("z")) or (i.startswith("x")): out_file.write(i) in_file.close() out_file.close() root@PC1:/home/test2# python test.py root@PC1:/home/test2# ls a.txt result.txt test.py root@PC1:/home/test2# cat result.txt ## 查看结果 x e j z c e z x e
4、提取以e结尾的数据
root@PC1:/home/test2# ls a.txt test.py root@PC1:/home/test2# cat a.txt d u s x e j z c e e f a z x e w f e root@PC1:/home/test2# cat test.py ## 提取以e结尾的数据 #/usr/bin/python in_file = open("a.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() for i in lines: if i.endswith("e\n"): out_file.write(i) in_file.close() out_file.close() root@PC1:/home/test2# python test.py root@PC1:/home/test2# ls a.txt result.txt test.py root@PC1:/home/test2# cat result.txt ## 查看结果 z c e z x e w f e
5、同时提取以e结尾或者j结尾的数据
root@PC1:/home/test2# ls a.txt test.py root@PC1:/home/test2# cat a.txt d u s x e j z c e e f a z x e w f e root@PC1:/home/test2# cat test.py ## 同时提取以e结尾或者以j结尾的数据 #/usr/bin/python in_file = open("a.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() for i in lines: if (i.endswith("e\n")) or (i.endswith("j\n")): out_file.write(i) in_file.close() out_file.close() root@PC1:/home/test2# python test.py root@PC1:/home/test2# ls a.txt result.txt test.py root@PC1:/home/test2# cat result.txt ## 查看结果 x e j z c e z x e w f e
6、提取以z开头且以e结尾的数据
root@PC1:/home/test2# ls a.txt test.py root@PC1:/home/test2# cat a.txt z u s x e j z c e e f a z x e w f e root@PC1:/home/test2# cat test.py ## 提取以z开头,且以e结尾的数据 #/usr/bin/python in_file = open("a.txt", "r") out_file = open("result.txt", "w") lines = in_file.readlines() for i in lines: if (i.startswith("z")) and (i.endswith("e\n")): out_file.write(i) in_file.close() out_file.close() root@PC1:/home/test2# python test.py root@PC1:/home/test2# ls a.txt result.txt test.py root@PC1:/home/test2# cat result.txt ## 查看结果 z c e z x e
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2021-05-22 c语言 8-8 求最大公约数
2021-05-22 c语言 8-7
2021-05-22 c语言 8-6
2021-05-22 c语言中利用递归求非负整数的阶乘
2021-05-22 c语言 8 - 5
2021-05-22 c语言 8-4
2021-05-22 c语言中枚举类型