读取以数字开头的行

Jal*_*501 2 python list file-handling readfile

我怎样才能读取文件中以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
Run Code Online (Sandbox Code Playgroud)

vks*_*vks 9

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


Kas*_*mvd 5

您可以使用isdigit()功能:

for line in open('f.txt','r'):
   if line[0].isdigit():
       print line
Run Code Online (Sandbox Code Playgroud)