我正在尝试编写将从文件中读取的示例代码 a)忽略空行 b)仅显示以dm开头的行 但它给我错误,不知道该怎么做, 任何人都可以给我一些亮点
def _find_dm_name():
with open (IOSTAT_OUTPUT,'r')as f:
for line in f:
lines = (line.rstrip() for line in f)
lines = list(line for line in lines if line)
if re.match("(dm-)", lines):
content=lines
return content
if __name__ == '__main__':
dm_name=_find_dm_name()
print dm_name
Traceback (most recent call last):
File "test.py", line 47, in <module>
dm_name=_find_dm_name()
File "test.py", line 41, in _find_dm_name
if re.match("(.*)", lines):
File "/usr/lib64/python2.6/re.py", line 137, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
即使我试试这个
def _find_dm_name():
with open (IOSTAT_OUTPUT,'r')as f:
for line in f:
if re.match("(dm-*)", line):
content=line
return content
它只给我最后一行
但是如何获得只匹配dm- +的所有行忽略任何空行