これがあなたが興味を持っているかもしれないpyparsingソリューションです。私はコードをかなりウォークスルーするためにコメントを追加しました。
data = """\
NameOfItemA
attribute1 = values1A
attribute2 = values2A
attributen = valuesnA
NameOfItemB
attribute1 = values1B
attribute2 = values2B
attributen = valuesnB
"""
from pyparsing import Suppress, Word, alphas, alphanums, \
empty, restOfLine, Dict, OneOrMore, Group
# define some basic elements - suppress the '=' sign because, while
# it is important during the parsing process, it is not an interesting part
# of the results
EQ = Suppress('=')
ident = Word(alphas, alphanums)
# an attribute definition is an identifier, and equals, and whatever is left
# on the line; the empty advances over whitespace so lstrip()'ing the
# values is not necessary
attrDef = ident + EQ + empty + restOfLine
# define a section as a lone ident, followed by one or more attribute
# definitions (using Dict will allow us to access attributes by name after
# parsing)
section = ident + Dict(OneOrMore(Group(attrDef)))
# overall grammar is defined as a series of sections - again using Dict to
# give us attribute-name access to each section's attributes
sections = Dict(OneOrMore(Group(section)))
# parse the string, which gives back a pyparsing ParseResults
s = sections.parseString(data)
# get data using dotted attribute notation
print s.NameOfItemA.attribute2
# or access data like it was a nested dict
print s.keys()
for k in s.keys():
print s[k].items()
プリント:
values2A
['NameOfItemB', 'NameOfItemA']
[('attribute2', 'values2B'), ('attribute1', 'values1B'), ('attributen', 'valuesnB')]
[('attribute2', 'values2A'), ('attribute1', 'values1A'), ('attributen', 'valuesnA')]