在python裡grep某個檔案的其中一行

看板Python作者時間17年前 (2008/05/13 14:38), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/2 (看更多)
我現在有一個txt檔案 假設叫tmb.txt 大概像這樣 def pion_gjets status: closed Total events: 535 當然稍微更長一點 但總之 我需要535這個數字 接在Total events後面的 我試著用 Line=os.system("cat tmb.txt | grep Total") 意圖再把Line split就可以拿到那個數字 問題是 這樣做的結果 Line永遠只傳回0 然後會把grep的結果 Total events: 535 印在螢幕上 我現在用非常笨的方法 readlines 然後找出有Total這行 再找到535這個數字 請問各位大大能否提供一些比較聰明的方法? 我更想知道的是像用os.system做ls, grep等等 怎麼能拿回結果 而不是回傳值0? 非常感謝^^ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 131.225.163.153

05/13 21:33, , 1F
import commands 然後 Line = commands.getoutput("cat....")
05/13 21:33, 1F
文章代碼(AID): #18AJToze (Python)
文章代碼(AID): #18AJToze (Python)