2009-05-06 4 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. 

を?

答えて

1

ordered dictionaryを使用して調べます。私はこれが公式のPythonではないと思っていますが、PEPにはリファレンス実装があります。

7

リストと同じように辞書の概念には意味がありません。代わりに、「end」が追加されないので、キー/値の挿入と削除に関して話す方が賢明です.Dictは順序付けされていません。あなたがdictsのdictsの辞書を持つようにしたいようなあなたの希望の出力から

は、それが見え、(すなわち{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':{'セクション:' .text '、'値 ':' 00000000 '、'行 ':' '、'タイプ ':' FUNC '、' Class ':' T '、' Size ':' 00000058 '}}、 ' _ashldi3.o ':{' __ashldi3 ':{'セクション ':' .text '、'値 ':' 00000000 ''_fixdfdi.o':{'__fixdfdi':{'セクション': 'ライン': ''、 'タイプ': 'FUNC'、 'クラス': 'T'、 'サイズ': '00000050'}} '.text'、 'Value': '00000000'、 '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '0000004c'} '__fixunsdfdi' : '' '' '' '' ''タイプ '' 'NOTYPE' ''クラス '' '' '' ''}}} – flight