2009-05-06 82 views
1

在附加列表中是可能的。但是,我如何在字典中添加附加内容?列出词典

Symbols from __ctype_tab.o: 
Name     Value Class  Type   Size  Line Section 
__ctype    |00000000| D |   OBJECT|00000004|  |.data 
__ctype_tab   |00000000| r |   OBJECT|00000101|  |.rodata 

Symbols from _ashldi3.o: 
Name     Value Class  Type   Size  Line Section 
__ashldi3   |00000000| T |    FUNC|00000050|  |.text 

Symbols from _ashrdi3.o: 
Name     Value Class  Type   Size  Line Section 
__ashrdi3   |00000000| T |    FUNC|00000058|  |.text 

Symbols from _fixdfdi.o: 
Name     Value Class  Type   Size  Line Section 
__fixdfdi   |00000000| T |    FUNC|0000004c|  |.text 
__fixunsdfdi  |  | U |   NOTYPE|  |  |*UND* 

我怎样才能创建像字典一样:

dictOfTables {'__ctype_tab.o':{'__ctype': Name:...,Value:...,Class:...,Type:...,Size:...,Line:...,Section:...}} etc. 

对于上面的文字?

回答

7

追加对词典的概念没有意义,与列表相同。相反,在插入和删除键/值方面讲话更加明智,因为没有“结尾”要附加到 - 字典是无序的。

从你期望的输出,它看起来像你想有类型的字典的类型的字典词典(即{filename : { symbol : { key:value }}我想你可以从你输入的是这样获得的:。

import re 

header_re = re.compile('Symbols from (.*):') 

def read_syms(f): 
    """Read list of symbols from provided iterator and return dict of values""" 
    d = {} 
    headings=None 
    for line in f: 
     line = line.strip() 
     if not line: return d # Finished. 

     if headings is None: 
      headings = [x.strip() for x in line.split()] 
      continue # First line is headings 

     items = [x.strip() for x in line.split("|")] 
     d[items[0]] = dict(zip(headings[1:], items[1:])) 
    return d 

f=open('input.txt') 
d={} 
for line in f: 
    m=header_re.match(line) 
    if m: 
     d[m.group(1)] = read_syms(f) 
+0

最后的字典看起来像这样: {'_ashrdi3.o':{'__ashrdi3':{'Section':'.text','Value':'00000000','Line':'','Type':'FUNC ','Class':'T','Size':'00000058'}}, '_ashldi3.o':{'__ashldi3':{'Section':'.text','Value':'00000000',' 'Line':'','Type':'FUNC','Class':'T','Size':'00000050'}}, '_fixdfdi.o':{'__fixdfdi':{'Section': '.text','Value':'00000000', 'Line':'','Type':'FUNC','Class':'T','Size':'0000004c'},'__fixunsdfdi':{'Section':'* UND *','Value' :'','Line':'','Type':'NOTYPE','Class':'U','Size':''}}} – flight 2009-05-06 12:38:58