如何在Python字典中转换包含以下输出的表?

2024-10-03 21:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

Filesystem           Type       1K-blocks      Used Available Use% Mounted on
/dev/sda             ext4          245671     47009    181459  21% /initlo
/dev/loop7           squashfs       39552     39552         0 100% /
none                 tmpfs          65536      1064     64472   2% /tmp
none                 aufs           65536      1064     64472   2% /dev
none                 tmpfs         510184        24    510160   0% /dev/shm
none                 aufs           65536      1064     64472   2% /etc
none                 aufs           65536      1064     64472   2% /var
none                 aufs           65536      1064     64472   2% /www
none                 aufs           65536      1064     64472   2% /mcl
none                 aufs           65536      1064     64472   2% /eds

想要创建一个字典(作为表头的键和值应该根据各自的列)。请帮助创建此词典。请点击链接获取该表的图片。你知道吗


Tags: devnoneontypeusedavailablefilesystemsda
3条回答

从@Rishikesh Jha(谢谢)那里得到灵感,下面是我建议的答案(不使用像pandas这样的高级库):

import re

INPUT = """Filesystem           Type       1K-blocks      Used Available Use%
Mounted on
/dev/sda             ext4          245671     47009    181459  21% /initlo
/dev/loop7           squashfs       39552     39552         0 100% /
none                 tmpfs          65536      1064     64472   2% /tmp
none                 aufs           65536      1064     64472   2% /dev
none                 tmpfs         510184        24    510160   0% /dev/shm
none                 aufs           65536      1064     64472   2% /etc
none                 aufs           65536      1064     64472   2% /var
none                 aufs           65536      1064     64472   2% /www
none                 aufs           65536      1064     64472   2% /mcl
none                 aufs           65536      1064     64472   2% /eds
"""

def split_spaces(s):
    "Split a string into a list, using spaces/tabs as separators"
    return re.split('\s+', s.strip())

def make_dict(input):
    "Convert a string containing a table into a list of dictionaries"
    input_list = input.splitlines()
    output_list = []
    # get the keys:
    keys = split_spaces(input_list[0])
    # get the list of dictionaries:
    for line in input_list[1:]:
        values = split_spaces(line)
        output_list.append({key: value for key,value in zip(keys, values)} )
    return output_list



print("Table: ", make_dict(INPUT))
row1 = d[0]
print("First row:", row1)
print("The type of first row is:", row1['Type'])

结果如下:

Table:  [{'Filesystem': 'Mounted', 'Type': 'on'}, {'Filesystem': '/dev/sda', 'Type': 'ext4', '1K-blocks': '245671', 'Used': '47009', 'Available': '181459', 'Use%': '21%'}, {'Filesystem': '/dev/loop7', 'Type': 'squashfs', '1K-blocks': '39552', 'Used': '39552', 'Available': '0', 'Use%': '100%'}, {'Filesystem': 'none', 'Type': 'tmpfs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%'}, {'Filesystem': 'none', 'Type': 'tmpfs', '1K-blocks': '510184', 'Used': '24', 'Available': '510160', 'Use%': '0%'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%'}]
First row: {'Filesystem': '/dev/sda', 'Type': 'ext4', '1K-blocks': 245671, 'Used': 47009, 'Available': 181459, 'Use%': '21%', 'Mounted': '/initlo', 'on': nan}
The type of first row is: ext4

split_spaces函数足够健壮,如果您有多个空格或制表符,甚至是两个空格或制表符的混合,那么它无论如何都可以工作。参见definition of ^{}in a regular expression;符号+表示“一个或多个”。你知道吗

在我的理解中,问题是“如何在Python字典中转换包含上述输出的字符串”?你知道吗

一种可能的解决方案是让pandas为您做步法(假设您的表在一个文件中):

import pandas
input_file = "input.txt"
t = pandas.read_csv(input_file, sep='\s+')

print("Here is the dictionary of dictionaries:")
d = t.to_dict(orient='index')
print("List of keys:", d.keys())
row1 = d[0]
print("First row:", row1)
print("The type of first row is:", row1['Type'])

诀窍在于分隔符参数(\s+:一个或多个空格/制表符)

这是输出:

Here is the dictionary of dictionaries:
List of keys: dict_keys([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
First row: {'Filesystem': '/dev/sda', 'Type': 'ext4', '1K-blocks': 245671, 'Used': 47009, 'Available': 181459, 'Use%': '21%', 'Mounted': '/initlo', 'on': nan}
The type of first row is: ext4

输出似乎是一个tab分隔的字符串/tab分隔的字符串列表。我会把完整的字符串改成一个字符串列表

def make_dict(input_list):
    final_output = []
    headers = input_list[0].strip().split("\t")
    for line in input_list[1:]:
        line = line.strip().split("\t")
        final_output.append({key: value for key,value in zip(headers, line)} )
    return final_output
Input_str = """Filesystem           Type       1K-blocks      Used Available Use% 
Mounted on
/dev/sda             ext4          245671     47009    181459  21% /initlo
/dev/loop7           squashfs       39552     39552         0 100% /
none                 tmpfs          65536      1064     64472   2% /tmp
none                 aufs           65536      1064     64472   2% /dev
none                 tmpfs         510184        24    510160   0% /dev/shm
none                 aufs           65536      1064     64472   2% /etc
none                 aufs           65536      1064     64472   2% /var
none                 aufs           65536      1064     64472   2% /www
none                 aufs           65536      1064     64472   2% /mcl
none                 aufs           65536      1064     64472   2% /eds
"""
input_list = Input_str.split("\n")
print(make_dict(input_list))

输出:

   [{'Filesystem': '/dev/sda', 'Type': '', '1K-blocks': 'ext4', 'Used': ' 245671', 'Available': '47009', 'Use%': '181459', 'Mounted on': '21%'}, {'Filesystem': '/dev/loop7', 'Type': 'squashfs', '1K-blocks': '39552', 'Used': '39552', 'Available': '0', 'Use%': '100%', 'Mounted on': '/'}, {'Filesystem': 'none', 'Type': 'tmpfs', '1K-blocks': ' 65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%', 'Mounted on': '/tmp'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%', 'Mounted on': '/dev'}, {'Filesystem': 'none', 'Type': 'tmpfs', '1K-blocks': '510184', 'Used': '', 'Available': '24', 'Use%': '510160', 'Mounted on': '0%t/dev/shm'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%', 'Mounted on': '/etc'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%', 'Mounted on': '/var'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%', 'Mounted on': '/www'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%', 'Mounted on': '/mcl'}, {'Filesystem': 'none', 'Type': 'aufs', '1K-blocks': '65536', 'Used': '1064', 'Available': '64472', 'Use%': '2%', 'Mounted on': '/eds'}]

相关问题 更多 >