Re: [問題] 過掉檔案某行數已刪文

看板Python作者 (Neisseria)時間11年前 (2014/09/27 17:23), 11年前編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
如果有一台 Unix 或 Linux 的主機在手邊 用 grep 應該是最簡單的 $ grep "^\d" file 或者,用 Perl one-liner,長一點點 $ perl -ne 'print if /^\d/;' file 如果要用 Python,可能是像這樣子 import re f = 'file' with open(f, 'r') as f: for line in f: if re.match(r'\d', line): print line, # , for tailed "\n" 要注意 re.match 一定要從字串開頭開始算,否則請改用 re.search 參考看看 ※ 引述《yshihyu (yshihyu)》之銘言: : https://gist.github.com/anonymous/be9247e9a5c63687e894 : 上面是原始資料 我想保留下面這樣數字格式 : https://gist.github.com/anonymous/62875442c02d1920a06f : 有什麼方便比較簡單可以濾過掉那些行數? : 謝謝 -- Happy Computing - Tips and recipes for Unix and programming http://cwchen123.tw/ Follow me at Twitter https://twitter.com/cwchen123 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.71.94.31 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1411809805.A.E06.html ※ 編輯: Neisseria (203.71.94.31), 09/27/2014 17:25:27

09/27 22:54, , 1F
^\d 不夠吧, pattern 還要再多一點
09/27 22:54, 1F

09/28 00:48, , 2F
請問一下re.match為什麼會過濾掉下面兩行裡面不是有數字
09/28 00:48, 2F

09/28 00:49, , 3F
09/28 00:49, 3F

09/28 04:12, , 4F
那兩行開頭不是數字呀...
09/28 04:12, 4F
文章代碼(AID): #1K9e8Du6 (Python)
文章代碼(AID): #1K9e8Du6 (Python)