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

作者: Neisseria (Neisseria)   2014-09-27 17:23:21
如果有一台 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
: 有什麼方便比較簡單可以濾過掉那些行數?
: 謝謝
作者: uranusjr (←這人是超級笨蛋)   2014-09-27 22:54:00
^\d 不夠吧, pattern 還要再多一點
作者: yshihyu (yshihyu)   2014-09-28 00:48:00
請問一下re.match為什麼會過濾掉下面兩行裡面不是有數字http://0rz.tw/qvTvD
作者: os653   2014-09-28 04:12:00
那兩行開頭不是數字呀...

Links booklink

Contact Us: admin [ a t ] ucptt.com