python中提取包含指定字符的行、提取以指定字符开头、指定字符结尾的行

1、测试数据

root@PC1:/home/test# ls
test.txt
root@PC1:/home/test# cat test.txt   ## 测试数据
w r s f
a s d g
z c w d
a d g t
z c e w

 

2、提取包含w的行

root@PC1:/home/test# ls
test.py  test.txt
root@PC1:/home/test# cat test.txt   ## 测试数据
w r s f
a s d g
z c w d
a d g t
z c e w
root@PC1:/home/test# cat test.py   ## 脚本
fp = open("test.txt", "r")      ## 打开文件
sample = fp.readlines()

file = open("result.txt", "w")   ## 指定写入文件

for line in sample:           ## 对每行进行遍历
    if "w" in line:            ## 利用if条件进行判断
        file.write(line)

fp.close()
file.close()
root@PC1:/home/test# python3 test.py    ## 执行命令
root@PC1:/home/test# ls   
result.txt  test.py  test.txt
root@PC1:/home/test# cat result.txt   ## 查看结果
w r s f
z c w d
z c e w

 

2、同时提取包含w和t的行:

root@PC1:/home/test# ls
test.py  test.txt
root@PC1:/home/test# cat test.txt
w r s f
a s d g
z c w d
a d g t
z c e w
root@PC1:/home/test# cat test.py
fp = open("test.txt", "r")
sample = fp.readlines()

file = open("result.txt", "w")

for line in sample:
    if "w" in line or "t" in line:     ## 同时提取包含w 或者 包含 t的行
        file.write(line)

fp.close()
file.close()
root@PC1:/home/test# python3 test.py
root@PC1:/home/test# ls
result.txt  test.py  test.txt
root@PC1:/home/test# cat result.txt    ## 查看结果
w r s f
z c w d
a d g t
z c e w

 

3、提取以w开头的行

root@PC1:/home/test# ls
test.py  test.txt
root@PC1:/home/test# cat test.txt    ## 测试数据
w r s f
a s d g
z c w d
a d g t
z c e w
root@PC1:/home/test# cat test.py    ## python脚本
fp = open("test.txt", "r")
sample = fp.readlines()

file = open("result.txt", "w")

for line in sample:
    if line.startswith("w"):    ## 提取以w开头的行
        file.write(line)

fp.close()
file.close()
root@PC1:/home/test# python3 test.py   ## 执行脚本
root@PC1:/home/test# ls
result.txt  test.py  test.txt
root@PC1:/home/test# cat result.txt      ## 查看结果
w r s f

 

4、同时提取以w开头 或者 以z开头的行

root@PC1:/home/test# ls
test.py  test.txt
root@PC1:/home/test# cat test.txt
w r s f
a s d g
z c w d
a d g t
z c e w
root@PC1:/home/test# cat test.py
fp = open("test.txt", "r")
sample = fp.readlines()

file = open("result.txt", "w")

for line in sample:
    if line.startswith("w") or line.startswith("z"):    ## 同时提取以w开头或者以z开头的行
        file.write(line)

fp.close()
file.close()
root@PC1:/home/test# python3 test.py
root@PC1:/home/test# ls
result.txt  test.py  test.txt
root@PC1:/home/test# cat result.txt         ## 查看结果
w r s f
z c w d
z c e w

 

5、提取以w结尾的行

root@PC1:/home/test# ls
test.py  test.txt
root@PC1:/home/test# cat test.txt   ## 测试数据
w r s f
a s d g
z c w d
a d g t
z c e w
root@PC1:/home/test# cat test.py
fp = open("test.txt", "r")
sample = fp.readlines()

file = open("result.txt", "w")

for line in sample:
    if line.endswith("w\n"):   ## 每行的末尾是换行符,所以提取最后字符为w的行为 w\n
        file.write(line)

fp.close()
file.close()
root@PC1:/home/test# python3 test.py
root@PC1:/home/test# ls
result.txt  test.py  test.txt
root@PC1:/home/test# cat result.txt   ## 查看结果
z c e w

 

6、提取以z开头同时又以g结尾的行

root@PC1:/home/test# ls
test.py  test.txt
root@PC1:/home/test# cat test.txt
w r s f
z s d g
z c w d
z d g g
z c e w
root@PC1:/home/test# cat test.py
fp = open("test.txt", "r")
sample = fp.readlines()

file = open("result.txt", "w")

for line in sample:
    if line.startswith("z") and line.endswith("g\n"):  ## 提取以z开头,同时以g结尾的行
        file.write(line)

fp.close()
file.close()
root@PC1:/home/test# python3 test.py
root@PC1:/home/test# cat result.txt        ## 查看结果
z s d g
z d g g

 

posted @ 2021-12-18 21:17  小鲨鱼2018  阅读(5458)  评论(0编辑  收藏  举报