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

前端 未结 1 781
猫巷女王i
猫巷女王i 2021-01-26 19:03

I getting data from subprocess command as a string. I want to store this data in a dict. How best do I achieve this?

Here is data example: (I have returned this as a s

相关标签:
1条回答
  • 2021-01-26 19:41

    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'}}
    
    0 讨论(0)
提交回复
热议问题