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
复制代码

 

posted @   小鲨鱼2018  阅读(1047)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!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语言中枚举类型
点击右上角即可分享
微信分享提示