Parsing an ad hoc tree
I have a flat file that looks like this:
Soccer
+Team:US
++Shirt:Red & White Stripes
++Shorts:Blue
++Players:17
+++Active:11
++++Forward:2
++++Midfield:4
++++Defense:4
++++Goalkeeper:1
+++Substitute:6
++++Forward:1
++++Midfied:2
++++Defense:3
++++Goalkeeper:
+Team:Mexico
++Shirt:Green
++Shorts:White
++Players:17
+++Active:11
++++Forward:3
++++Midfield:3
++++Defense:4
++++Goalkeeper:1
+++Substitute:6
++++Forward:2
++++Midfield:1
++++Defense:2
++++Goalkeeper:1
What's the most efficient way to parse this into a data structure in Python? Or else, how would I convert this to XML, JSON, or a simple Python object?
The resulting data structure must support some type of query, for example in psuedocode, for example:
Soccer[Team='US'][Shirt]
should be able to return "Red and White Stripes"
Similarly, something of order,
Soccer[Team='US'][Players][Substitute][Goalkeeper]
should return None
Is there a module that would be helpful here?
source to share
For such a thing, you can use Marpa :: R2 , Perl for Marpa, general BNF parser - parser , parse tree . Some more explanations in my answer to a related question Handling a re-structured text file with python
Example ast_traverse()
: Parses values ββfrom a block of text based on specific keys
source to share