看板 Python 關於我們 聯絡資訊
我現在有一個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
rofu:import commands 然後 Line = commands.getoutput("cat....") 05/13 21:33