本文介绍了Python:将表转换成字符串并转换成键:值对并存储在dict中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 string from subprocess.)

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},
...
}

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

Here is the method I am trying to implement to sort this data

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方法不正确

Note 1:I know that the zip method is incorrect

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

Note 2:I don't know how to account for items that are - e.g. when '\n' comes in after 'disk'.

推荐答案

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

Using itertools.zip_longest --> If using 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'}


根据评论编辑


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)

输出:

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

这篇关于Python:将表转换成字符串并转换成键:值对并存储在dict中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 10:10