Python: Convert table to string to key:value pairs and store in dict

谁都会走 提交于 2019-12-02 03:55:45

Using itertools.zip_longest --> If using python2 izip_longest

Ex:

from itertools import zip_longest


data = """NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0     7:0    0 140.7M  1 loop /snap/gnome-3-26-1604/82
loop1     7:1    0  89.3M  1 loop /snap/core/6673
sda       8:0    0    11G  0 disk 
├─sda1    8:1    0  10.9G  0 part /
├─sda14   8:14   0     4M  0 part 
└─sda15   8:15   0   106M  0 part /boot/efi"""

result = []
header = None
for line in data.splitlines():
    line = line.strip().split()
    if not header:
        header = line
    else:
        result.append(dict(zip_longest(header, line, fillvalue="")))  

Output:

{'NAME': 'loop0', 'MAJ:MIN': '7:0', 'RM': '0', 'SIZE': '140.7M', 'RO': '1', 'TYPE': 'loop', 'MOUNTPOINT': '/snap/gnome-3-26-1604/82'}
{'NAME': 'loop1', 'MAJ:MIN': '7:1', 'RM': '0', 'SIZE': '89.3M', 'RO': '1', 'TYPE': 'loop', 'MOUNTPOINT': '/snap/core/6673'}
{'NAME': 'sda', 'MAJ:MIN': '8:0', 'RM': '0', 'SIZE': '11G', 'RO': '0', 'TYPE': 'disk', 'MOUNTPOINT': ''}
{'NAME': '├─sda1', 'MAJ:MIN': '8:1', 'RM': '0', 'SIZE': '10.9G', 'RO': '0', 'TYPE': 'part', 'MOUNTPOINT': '/'}
{'NAME': '├─sda14', 'MAJ:MIN': '8:14', 'RM': '0', 'SIZE': '4M', 'RO': '0', 'TYPE': 'part', 'MOUNTPOINT': ''}
{'NAME': '└─sda15', 'MAJ:MIN': '8:15', 'RM': '0', 'SIZE': '106M', 'RO': '0', 'TYPE': 'part', 'MOUNTPOINT': '/boot/efi'}

Edit as per comment

from itertools import zip_longest


data = """NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0     7:0    0 140.7M  1 loop /snap/gnome-3-26-1604/82
loop1     7:1    0  89.3M  1 loop /snap/core/6673
sda       8:0    0    11G  0 disk 
├─sda1    8:1    0  10.9G  0 part /
├─sda14   8:14   0     4M  0 part 
└─sda15   8:15   0   106M  0 part /boot/efi"""

result = {}
header = None
c = 1
for line in data.splitlines():
    line = line.strip().split()
    if not header:
        header = line
    else:
        key = "block device {}".format(c)
        result.update({key: dict(zip_longest(header, line, fillvalue=""))})  
        c += 1

print(result)

Output:

{'block device 1': {'MAJ:MIN': '7:0',
                    'MOUNTPOINT': '/snap/gnome-3-26-1604/82',
                    'NAME': 'loop0',
                    'RM': '0',
                    'RO': '1',
                    'SIZE': '140.7M',
                    'TYPE': 'loop'},
 'block device 2': {'MAJ:MIN': '7:1',
                    'MOUNTPOINT': '/snap/core/6673',
                    'NAME': 'loop1',
                    'RM': '0',
                    'RO': '1',
                    'SIZE': '89.3M',
                    'TYPE': 'loop'},
 'block device 3': {'MAJ:MIN': '8:0',
                    'MOUNTPOINT': '',
                    'NAME': 'sda',
                    'RM': '0',
                    'RO': '0',
                    'SIZE': '11G',
                    'TYPE': 'disk'},
 'block device 4': {'MAJ:MIN': '8:1',
                    'MOUNTPOINT': '/',
                    'NAME': '├─sda1',
                    'RM': '0',
                    'RO': '0',
                    'SIZE': '10.9G',
                    'TYPE': 'part'},
 'block device 5': {'MAJ:MIN': '8:14',
                    'MOUNTPOINT': '',
                    'NAME': '├─sda14',
                    'RM': '0',
                    'RO': '0',
                    'SIZE': '4M',
                    'TYPE': 'part'},
 'block device 6': {'MAJ:MIN': '8:15',
                    'MOUNTPOINT': '/boot/efi',
                    'NAME': '└─sda15',
                    'RM': '0',
                    'RO': '0',
                    'SIZE': '106M',
                    'TYPE': 'part'}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!