List of dictionaries
You can add a list to the list. But how do I achieve dictionary addition?
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 to create a dictionary like:
dictOfTables {'__ctype_tab.o':{'__ctype': Name:...,Value:...,Class:...,Type:...,Size:...,Line:...,Section:...}} etc.
for the above text?
+1
a source to share
2 answers
Adding doesn't make sense for a dictionary concept as well as for a list. Instead, it makes more sense to speak in terms of key / values insertion and deletion, since there is no "end" to add - the dict is out of order.
From your desired output, it looks like you want to have a dicts (i.e. {filename : { symbol : { key:value }}
) voice recorder . 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)
+7
a source to share
Take a look at the ordered dictionary . I don't think this is already in official Python, but there is a referenced implementation available in the PEP.
+1
a source to share