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 @   小鲨鱼2018  阅读(6135)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-12-18 python中统计列表中元素出现的次数
2020-12-18 python中列表元素求交集和并集
2020-12-18 python中列表的去重复和取重复
2020-12-18 python中成员操作符
2020-12-18 python中列表分片
2020-12-18 pyhton中实现列表元素顺序颠倒
2020-12-18 python中取列表的后半部分元素
点击右上角即可分享
微信分享提示