views:

154

answers:

2
+1  Q: 

List a dictionary

In a list appending is possible. But how I achieve appending in dictionary?

    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*

How can I create a dictionary like:

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

for the above text?

+1  A: 

Look into using an ordered dictionary. I don't think this is in official Python yet, but there's a reference implementation available in the PEP.

unwind
+7  A: 

Appending doesn't make sense to the concept of dictionary in the same way as for list. Instead, it's more sensible to speak in terms of inserting and removing key/values, as there's no "end" to append to - the dict is unordered.

From your desired output, it looks like you want to have a dict of dicts of dicts, (ie {filename : { symbol : { key:value }}. I think you can get this from your input with something like this:

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)
Brian
The final dict would look like this:{'_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