哪位大佬可以幫我做個小腳本,文本提取含有關鍵詞的行,例:a.tx檔案中含有“PT”“2 0 3”這兩個關鍵詞的行都提取出來
a.txt:
1.2 0 3 8 89
3.2.2 0 4 5 56
4. 0 4 5 56
5. 0 4 5 56PT
6.2 0 4 5 56
7.2 0 3 5 562 0 4 5 56
8.2 0 4 5 56PT
9.2 0 3 5 56
10.2 0 4 5 56
提取的檔案:
1.2 0 3 8 89
5. 0 4 5 56PT
7.2 0 3 5 562 0 4 5 56
8.2 0 4 5 56PT
9.2 0 3 5 56
uj5u.com熱心網友回復:
def test_txt():
file_data = open('a.txt')
for l in file_data.readlines():
if l.find('2 0 3') >=0 or l.find('PT') >= 0:
print(l.strip())
test_txt()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/139497.html
