表格形式的文件内容需要在python中的字典中转换

时间:2014-07-24 05:44:01

标签: python

abc.txt文件内容为 -

+--------------------------------------+-----------+--------------+------+-------------+----------+-------------+
|                  ID                  |   Status  | Display Name | Size | Volume Type | Bootable | Attached to |
+--------------------------------------+-----------+--------------+------+-------------+----------+-------------+
| 43c51829-20f8-422d-a667-ce2ed917a33c |  creating |   New-Vol    |  2   |     None    |  false   |             |
| 7b388ad1-eec9-44fc-b9b1-240c0681d106 | available |   New-Vol    |  2   |     None    |  false   |             |
| d4649bda-eb4f-40f9-a856-254f51f274ae | available |   New-Vol    |  2   |     None    |  false   |             |
+--------------------------------------+-----------+--------------+------+-------------+----------+-------------+

如何使用python将此内容转换为有效字典?

试过代码 -

def _table2dict():
#    f = open('abc.txt', 'wt')
#    f.write(body)
#    f.close()
    table = [line.strip().split('|') for line in open("abc.txt", 'r')]
    del table[0]
    del table[1]
    del table[-1]

    result = {'volumes' : []}
    for a_row in table[1:]:
        tmp = {}
        for key, value in zip(table[0][1:], a_row[1:]):
            key = key.strip(' ')
            value = value.strip(' ')
            tmp[key] = value
        result["volumes"].append(tmp)
    return result

x = _table2dict()
print x

我试过上面的命令,它提供了某种输出。

1 个答案:

答案 0 :(得分:0)

你可以尝试这个(可能不是可以想象的最漂亮的python代码):

def _table2dict():
    entries = {'volumes' : []}
    fields = ()
    for line in open("abc.txt",'r'):
        entry = {}
        if not line.strip().startswith("+-"): # get rid of +--- lines                                                                                                                                                             
            cells = [x.strip() for x in line.split("|")[1:-1] ] # weed out first and last | and get rid of whitespace                                                                                                                        
            if len(fields) == 0: # first get field names if we don't have it already
                fields = [cell for cell in cells]
                # do not process this line and skip to next in file
                continue

            if len( fields) != 0: # we already found the field names                                                                                                                                                              
                for (key,value) in zip(fields, cells):
                    entry[key] = value
                entries["volumes"].append(entry)

    return entries

x = _table2dict()
print x

输出(为便于阅读而格式化):

{'volumes': 
    [
      {'Status': 'creating', 'Bootable': 'false', 'Attached to': '', 'Display Name': 'New-Vol', 'Volume Type': 'None', 'ID': '43c51829-20f8-422d-a667-ce2ed917a33c', 'Size': '2'}, 
      {'Status': 'available', 'Bootable': 'false', 'Attached to': '', 'Display Name': 'New-Vol', 'Volume Type': 'None', 'ID': '7b388ad1-eec9-44fc-b9b1-240c0681d106', 'Size': '2'}, 
      {'Status': 'available', 'Bootable': 'false', 'Attached to': '', 'Display Name': 'New-Vol', 'Volume Type': 'None', 'ID': 'd4649bda-eb4f-40f9-a856-254f51f274ae', 'Size': '2'}
    ]
}

请注意,除了让最终输出为dict之外,我还为每个条目创建了一个dict。因此,字段的顺序不是原始文件中的顺序,而是可以通过标题中给出的名称检索每个字段。

这是否符合你的想法?

编辑:在第一个版本中,我在输出中有一个额外的空字段,因为最后一个' |'我没有照顾的角色,现在已经修好了