我需要将df
命令的输出转换为python字典格式
Filesystem 1K-blocks Used Available Use% Mounted on
udev 1987884 0 1987884 0% /dev
tmpfs 402060 1992 400068 1% /run
tmpfs 402060 1992 400068 1% /run
/dev/sda6 113960124 37751712 70376476 35% /
我需要的格式是:
[{'Filesystem':udev, 'Used':0, 'Available':1987884 ,'Use%':0%, 'Mounted_On':'/dev'},{'Filesystem':udev, 'Used':0, 'Available':1987884 ,'Use%':0%, 'Mounted_On':'/dev'}]
我试图实现这一点:
path='df -h'
p = Popen(path, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = p.stdout.read().decode() # from bytes to str
header, data, _*= output.split('\n') # split to lines
header = header.replace('Mounted on', 'Mounted_on') # fix a two word field
df = dict(list(zip(header.split(), data.split()))) # split to fields and combine
return df
但它只返回第一行:
{'Filesystem': 'udev', 'Size': '1.9G', 'Used': '0', 'Avail': '1.9G', 'Use%': '0%', 'Mounted_on': '/dev'}
我可以知道丢失的代码是什么吗。我认为其中有些逻辑错误
第一个问题是:
这将把
output
的第一行解压为header
,第二行解压为data
,第三行解压为_
-但是您有3行以上的行,并且很可能希望所有行(除了头)都在data
中,因此:但这也需要改变
到
相关问题 更多 >
编程相关推荐