读取以数字开头的行

时间:2014-12-17 09:18:29

标签: python list readfile file-handling

我怎样才能读取文件中以python中的数字开头的行,即

Hello World <--ignore
Lovely day
blah43 
blah
1234 <--read
blah12 <--ignore
blah 
3124 <--read
Not matter how many words there are <--ignore
blah 
0832 8423984 234892304 8239048324 8023948<--read
blah132 <--ignore

2 个答案:

答案 0 :(得分:8)

import re
with open("filename") as f:
    for line in f:
        if re.match(r"^\d+.*$",line):
            print line

答案 1 :(得分:3)

您可以使用isdigit()功能:

for line in open('f.txt','r'):
   if line[0].isdigit():
       print line
相关问题