I know I’ve seen a good algorithm for this, but having trouble finding it.
I’ve got a (poorly formatted) output from a tool (whose output style I cannot control) that I need to parse.
It looks something like this:
NameOfItemA
attribute1 = values1
attribute2 = values2
...
attributen = valuesn
NameOfItemB
attribute1 = values1
attribute2 = values2
...
attributen = valuesn
Where NameOfItemX and attributeX is a clearly defined set of known names. Need to turn it into reasonable objects:
ObjectForA.attribute1 = values1
etc.
I know I’ve done this before, just cant remember how I did it. It looked something like:
for line in textinput:
if line.find("NameOfItem"):
... parse until next one ...
Hopefully what I’m saying makes sense and someone can help
what about:
Of course, you could ommit the basic object if you already have a class you want to use…
After all is said and done, you have a dictionary of objects with keys=object_names and values = attributes (as strings — You’ll need to convert to whatever type it is supposed to be if it’s not a string)
Also note that this input file format looks A LOT like that of the
ConfigParsermodule. You could probably read the file and change the"NameOfItem"lines to[item]lines and pass that to ConfigParser as a StringIO object…