如何从分隔列表创建关联数组?

时间:2018-08-16 15:09:14

标签: python multidimensional-array

我正在以这种格式处理来自存储设备的读数中的数据:

id:name:UPS_serial_number:WWNN:status:IO_group_id:IO_group_name:config_node:UPS_unique_id:hardware:iscsi_name:iscsi_alias:panel_name:enclosure_id:canister_id:enclosure_serial_number:site_id:site_name
10:node_A::00A550:online:0:io_grp0:yes::SV1:iqn.1986-03.com:2145.test.nodeA::A:::::
15:node_B::00A548:online:0:io_grp0:no::SV1:iqn.1986-03.com.:2145.test.nodeB::B:::::

如何将数据作为2D数组读取,例如datarray['15']['status']

我尝试过这种方式:

# Create array
datarray = []
try:
    # Loop trough list
    for i, x in enumerate(lis):
        # Split on the delimter
        linesplit = x.split(":")
        row = []
        for lsi,lsx in enumerate(linesplit):
            row.append([lsi,lsx])
        datarray.append(row)

但这似乎将数据切片错误:

[[[0, u'id'], [1, u'name'], [2, u'UPS_serial_number'], [3, u'WWNN'], [4, u'status'], [5, u'IO_group_id'], [6, u'IO_group_name'], [7, u'config_node'], [8, u'UPS_unique_id'], [9, u'hardware'], [10, u'iscsi_name'], [11, u'iscsi_alias'], [12, u'panel_name'], [13, u'enclosure_id'],

2 个答案:

答案 0 :(得分:1)

我可以从数据中得出的是它是用冒号(:)分隔的数据,第一行具有标题。如果是这种情况,您可以在使用分隔符=':'加载csv文件时将其加载到pandas数据帧。然后将该数据帧转换为numpy数组。

import pandas as pd
import os
os.chdir('/Users/Downloads/')
df = pd.read_csv('train.txt',sep=':')
df

id  name    UPS_serial_number   WWNN    status  IO_group_id IO_group_name   config_node UPS_unique_id   hardware    iscsi_name  iscsi_alias panel_name  enclosure_id    canister_id enclosure_serial_number site_id site_name
10  node_A  NaN 00A550  online  0   io_grp0 yes NaN SV1 iqn.1986-03.com 2145.test.nodeA NaN A   NaN NaN NaN NaN NaN
15  node_B  NaN 00A548  online  0   io_grp0 no  NaN SV1 iqn.1986-03.com.    2145.test.nodeB NaN B   NaN NaN NaN NaN NaN

df.as_matrix()

array([['node_A', nan, '00A550', 'online', 0, 'io_grp0', 'yes', nan,
        'SV1', 'iqn.1986-03.com', '2145.test.nodeA', nan, 'A', nan, nan,
        nan, nan, nan],
       ['node_B', nan, '00A548', 'online', 0, 'io_grp0', 'no', nan,
        'SV1', 'iqn.1986-03.com.', '2145.test.nodeB', nan, 'B', nan, nan,
        nan, nan, nan]], dtype=object)

答案 1 :(得分:1)

使用csv.DictReader将各行作为字典读取,然后使用字典理解来创建“外部”字典,将ID属性映射到具有该ID的内部字典。

raw = """id:name:UPS_serial_number:WWNN:status:IO_group_id:IO_group_name:config_node:UPS_unique_id:hardware:iscsi_name:iscsi_alias:panel_name:enclosure_id:canister_id:enclosure_serial_number:site_id:site_name
10:node_A::00A550:online:0:io_grp0:yes::SV1:iqn.1986-03.com:2145.test.nodeA::A:::::
15:node_B::00A548:online:0:io_grp0:no::SV1:iqn.1986-03.com.:2145.test.nodeB::B:::::"""

reader = csv.DictReader(raw.splitlines(), delimiter=":")
result = {line["id"]: line for line in reader}
print(result["15"]["status"])  # 'online'

请注意,这不是2D数组,而是字典字典(字典是关联数组)。作为简单的2D数组,像result["15"]["status"]这样的查询将不起作用。