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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM