Python:将表转换为字符串并转换为键:值对并存储在dict中

时间:2019-07-12 09:14:34

标签: python dictionary list-comprehension dictionary-comprehension

我从子过程命令获取数据作为字符串。我想将此数据存储在字典中。我如何才能最好地做到这一点?

这是数据示例:(我已经从子流程中将其作为字符串返回了。)

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

这是我想要的输出:

{block device 1: 
    { "name" :  value,
    "maj:min" : value,
    "RM" : value,
    "SIZE" : value,
    "RO": value,
    "TYPE": value,
    "MOUNTPOINT" : value},
 block device 2: 
     { "name" :  value,
    "maj:min" : value,
    "RM" : value,
    "SIZE" : value,
    "RO": value,
    "TYPE": value,
    "MOUNTPOINT" : value},
...
}

这是我尝试实现的对这些数据进行排序的方法

def multiple_column_dict(a_string):
    a_dict = {}

    lines = re.split("\n", a_string)
    for l in lines:
        l = re.split(" +", l)
        a_dict = dict(zip(l[::2], l[1::2]))
    return a_dict

注1: 我知道zip方法不正确

注2: 我不知道该如何计算-例如当'\ n'出现在'disk'之后。

1 个答案:

答案 0 :(得分:0)

使用itertools.zip_longest->如果使用python2 izip_longest

例如:

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="")))  

输出:

{'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'}

根据评论编辑

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)

输出:

{'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'}}