gpt4 book ai didi

Python将表转换为字典

转载 作者:行者123 更新时间:2023-12-02 00:57:44 24 4
gpt4 key购买 nike

问题:一个命令生成一个表格,这使得编写脚本变得非常困难。

解决方案:将表格转换为 Python 字典,以便更有效地使用。这些表可以有 1 - 20 个不同的虚拟驱动器,并且不能设置“名称”等属性。

示例表:

Virtual Drives = 4

VD LIST :
=======

----------------------------------------------------------
DG/VD TYPE State Access Consist Cache sCC Size Name
----------------------------------------------------------
0/0 RAID1 Optl RW No RWTD - 1.818 TB one
1/1 RAID1 Optl RW No RWTD - 1.818 TB two
2/2 RAID1 Optl RW No RWTD - 1.818 TB three
3/3 RAID1 Optl RW No RWTD - 1.818 TB four
4/4 RAID10 Reblg RW No RWTD - 4.681 TB
----------------------------------------------------------

示例字典:

{"DG/VD":"0/0", "TYPE":"RAID1", "State":"Optl", "Access":"RW", "Consist":"No", "Cache":"RWTD", "sCC":"-", "Size":"1.818 TB", "Name":"one"}
{"DG/VD":"4/4", "TYPE":"RAID10", "State":"Reblg", "Access":"RW", "Consist":"No", "Cache":"RWTD", "sCC":"-", "Size":"4.681 TB", "Name":""}

总共有四个词典,每个虚拟驱动器一个。如何最好地解决这个问题?

我有一些想法。首先,搜索表头并按空格拆分以定义列表。其次,搜索带有“编号/编号”的虚拟驱动器并按空格拆分以定义第二个列表。但是,“大小”需要特殊,因为它需要忽略数字和“TB”之间的空格。

接下来,将两个列表压缩在一起以生成一个字典。有没有人对处理此文本有更好的想法?

# Create a list of all the headers in the virtual disk table
get_table_header = " /c0/vall show | awk '/^DG\/VD/'"
table_header_values = console_info(utility + get_table_header).split()

['DG/VD', 'TYPE', 'State', 'Access', 'Consist', 'Cache', 'sCC', 'Size', 'Name']

# Create a list of all virtual drives
get_virtual_drives = " /c0/vall show | awk '/^[0-9]\/[0-9]/'"
virtual_drive_values = console_info(utility + get_virtual_drives).split()

["0/0", "RAID1", "Optl", "RW", "No", "RWTD", "-", "1.818", "TB", "0"]

最佳答案

from itertools import dropwhile, takewhile
with open("test.txt") as f:
dp = dropwhile(lambda x: not x.startswith("-"), f)
next(dp) # skip ----
names = next(dp).split() # get headers names
next(f) # skip -----
out = []
for line in takewhile(lambda x: not x.startswith("-"), f):
a, b = line.rsplit(None, 1)
out.append(dict(zip(names, a.split(None, 7) + [b])))]

输出:

from pprint import  pprint as pp

pp(out)
[{'Access': 'RW',
'Cache': 'RWTD',
'Consist': 'No',
'DG/VD': '0/0',
'Name': 'one',
'Size': '1.818 TB',
'State': 'Optl',
'TYPE': 'RAID1',
'sCC': '-'},
{'Access': 'RW',
'Cache': 'RWTD',
'Consist': 'No',
'DG/VD': '1/1',
'Name': 'two',
'Size': '1.818 TB',
'State': 'Optl',
'TYPE': 'RAID1',
'sCC': '-'},
{'Access': 'RW',
'Cache': 'RWTD',
'Consist': 'No',
'DG/VD': '2/2',
'Name': 'three',
'Size': '1.818 TB',
'State': 'Optl',
'TYPE': 'RAID1',
'sCC': '-'},
{'Access': 'RW',
'Cache': 'RWTD',
'Consist': 'No',
'DG/VD': '3/3',
'Name': 'four',
'Size': '1.818 TB',
'State': 'Optl',
'TYPE': 'RAID1',
'sCC': '-'}]

如果你想维持秩序使用 OrderedDict

out = [OrderedDict(zip(names, line.split()))
for line in takewhile(lambda x: not x.startswith("-"), f)]

对于根据您的编辑缺少的名称值:

from itertools import dropwhile, takewhile

with open("test.txt") as f:
dp = dropwhile(lambda x: not x.startswith("-"), f)
next(dp) # skip ----
names = next(dp).split() # get headers names
next(f) # skip -----
out = []
for line in takewhile(lambda x: not x.startswith("-"), f):
a, b = line.rsplit(" ", 1)
out.append(dict(zip(names, a.rstrip().split(None, 7) + [b.rstrip()])))

输出:

[{'Access': 'RW',
'Cache': 'RWTD',
'Consist': 'No',
'DG/VD': '0/0',
'Name': 'one',
'Size': '1.818 TB',
'State': 'Optl',
'TYPE': 'RAID1',
'sCC': '-'},
{'Access': 'RW',
'Cache': 'RWTD',
'Consist': 'No',
'DG/VD': '1/1',
'Name': 'two',
'Size': '1.818 TB',
'State': 'Optl',
'TYPE': 'RAID1',
'sCC': '-'},
{'Access': 'RW',
'Cache': 'RWTD',
'Consist': 'No',
'DG/VD': '2/2',
'Name': 'three',
'Size': '1.818 TB',
'State': 'Optl',
'TYPE': 'RAID1',
'sCC': '-'},
{'Access': 'RW',
'Cache': 'RWTD',
'Consist': 'No',
'DG/VD': '3/3',
'Name': 'four',
'Size': '1.818 TB',
'State': 'Optl',
'TYPE': 'RAID1',
'sCC': '-'},
{'Access': 'RW',
'Cache': 'RWTD',
'Consist': 'No',
'DG/VD': '4/4',
'Name': '',
'Size': '4.681 TB',
'State': 'Reblg',
'TYPE': 'RAID10',
'sCC': '-'}]

这还将处理 TB 和 Name 列值之间有多个空格的行 1.818 TB one

关于Python将表转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32510653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com