解析命令输出-Python
我正在运行一个实用程序,它解析解析命令输出-Python,python,regex,Python,Regex,我正在运行一个实用程序,它解析df命令的输出。我捕获输出并将其发送到解析器。以下是一个示例: Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on /dev/disk2 1996082176 430874208 1564695968 22% 2429281 4292537998 0% / devfs
df
命令的输出。我捕获输出并将其发送到解析器。以下是一个示例:
Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on
/dev/disk2 1996082176 430874208 1564695968 22% 2429281 4292537998 0% /
devfs 668 668 0 100% 1156 0 100% /dev
map -hosts 0 0 0 100% 0 0 100% /net
map auto_home 0 0 0 100% 0 0 100% /home
下面是函数:
def parse_df(self, content):
"""Parse the `df` content output
:param content: The command content output
:return: (list) A list of objects of the type being parsed
"""
entries = []
if not content:
return entries
# Split the content by line and check if we should ignore first line
for line in content.split("\n"):
if line.startswith("Filesystem"):
continue
tokens = line.split()
print tokens
但是,我得到了以下输出:
['/dev/disk2', '1996082176', '430876480', '1564693696', '22%', '2429288', '4292537991', '0%', '/']
['devfs', '668', '668', '0', '100%', '1156', '0', '100%', '/dev']
['map', '-hosts', '0', '0', '0', '100%', '0', '0', '100%', '/net']
['map', 'auto_home', '0', '0', '0', '100%', '0', '0', '100%', '/home']
问题是map-host
应该是单个元素(对于Filesystem
列)。
我尝试应用正则表达式tokens=re.split(r'\s{2,}',line)
,但结果仍然不正确:
['/dev/disk2', '1996082176 430869352 1564700824', '22% 2429289 4292537990', '0%', '/']
解析输出的正确方法是什么?因为FS可能会有多个空格,并且只要您可以预先确定可以使用不同的分隔符拆分并最终合并它们
fs, rest = re.split(r'\s{2,}', line, 1)
result = [fs] + rest.split()
但是如果fs
像一个大的空间一样被一个单独的空间隔开,这就行不通了
同意使用
os.statvfs(path)
是更好的工具的评论df
将是一个子流程
调用。如果这是您想要的行为,我能看到的最简单的方法是加入数组的第一个元素,直到您到达一个数字元素
比如说:
tokens = line.split()
n = 1
while n < len(tokens) and not tokens[n].isdigit():
n += 1
tokens[0] = ' '.join(tokens[:n])
tokens = [ tokens[0] ] + tokens[n:]
只需在一个或多个后跟数字或
/
>>> import re
>>> s = '''/dev/disk2 1996082176 430874208 1564695968 22% 2429281 4292537998 0% /
devfs 668 668 0 100% 1156 0 100% /dev
map -hosts 0 0 0 100% 0 0 100% /net
map auto_home 0 0 0 100% 0 0 100% /home'''.splitlines()
>>> for line in s:
print re.split(r'\s+(?=[\d/])', line)
['/dev/disk2', '1996082176', '430874208', '1564695968', '22%', '2429281', '4292537998', '0%', '/']
['devfs', '668', '668', '0', '100%', '1156', '0', '100%', '/dev']
['map -hosts', '0', '0', '0', '100%', '0', '0', '100%', '/net']
['map auto_home', '0', '0', '0', '100%', '0', '0', '100%', '/home']
>>>
您需要使用不同的分隔符,如
\t
?即使有多个空格也可以。每列都有固定的宽度。您可以尝试基于that@Nishant:按\t
拆分:['/dev/disk2 1996082176 430874728 15646954448 22%2429300 4292537979 0%/']
听起来像是正则表达式的作业;或者。不相关,但是有一些系统调用(例如STATVF)可能会更直接地得到您想要的。
>>> import re
>>> s = '''/dev/disk2 1996082176 430874208 1564695968 22% 2429281 4292537998 0% /
devfs 668 668 0 100% 1156 0 100% /dev
map -hosts 0 0 0 100% 0 0 100% /net
map auto_home 0 0 0 100% 0 0 100% /home'''.splitlines()
>>> for line in s:
print re.split(r'\s+(?=[\d/])', line)
['/dev/disk2', '1996082176', '430874208', '1564695968', '22%', '2429281', '4292537998', '0%', '/']
['devfs', '668', '668', '0', '100%', '1156', '0', '100%', '/dev']
['map -hosts', '0', '0', '0', '100%', '0', '0', '100%', '/net']
['map auto_home', '0', '0', '0', '100%', '0', '0', '100%', '/home']
>>>