I've been looking for a way to parse a simple XML-like language for use as a type of data storage. I've been through stuff like JSON, XML, etc but I don't want to use them because they are quite slow. I just need a simple way to parse this:
[stuff]
[key1]data[/key1]
[key2]data[/key2]
[/stuff]
And make/map it into a dictionary, like this:
{"stuff":{"key1":"data", "key2":"data"}}
I've made myself a generator which will process a dictionary according to the syntax rules:
def generate_di(self, item)
assert type(item) is dict
for key in item:
if type(item[key]) is dict:
self.puts(self.strtag % key)
# means we are going in one branch
self.rlevel += 1
self.generate_di(item[key])
# we have finished parsing that branch
self.rlevel -= 1
self.puts(self.endtag % key)
else:
self.puts(self.strtag % key + str(item[key]) + self.endtag % key)
Which takes in a dictionary, say {"deltas":{"key":"data", "key2":"data"}}
and puts
this into the stream:
[deltas]
[key]data[/key]
[key2]data[/key2]
[/deltas]
As you can see this is very XML-like but eliminates the complex tree and attributes arguments. Any help would be appreciated!