Hi all, I have the following code - >>> j = """a = { b = { c = { d = 5 e = 10 } } }""" >>> ref = [] >>> data = {} >>> ref.append(data) >>> for line in j.split('\n'): ... if "=" in line: ... (LHS, RHS) = line.split(" = ") ... if RHS == "{": ... #open group ... ref[-1][LHS] = {} ... ref.append(ref[-1][LHS]) ... else: ... ref[-1][LHS] = RHS ... else: ... #Only other value in test data is "}", so close group ... ref.pop(-1)
Fairly unreadable huh. Basically, I'm using the list to store dictionary references, so as to create a bunch of nested dictionaries, working on the principle of last in, first off. I guess it's a rudimentary stack or thereabouts. Basically, if there's any caveats with popping and/or lists of references, I'm dead keen to find out before I take my prototype any further. Alternatively, if there's a far more simpler way to do this, also keen to hear it. :) (My first version had a list of keys ["a", "b", "c", "d"] and would use that to track depth. I then had a function which looked like this - if len(keyList) - 1 == -1: return elif len(keyList) - 1 == 0: return fooDict[keyList[0]] elif len(keyList) - 1 == 1: return fooDict[keyList[0]][keyList[1]] ... ... elif len(keyList) -1 == 5: return fooDict[keyList[0]][keyList[1]][keyList[2]][keyList[3]][keyList[4]][keyList[5]] And so forth.... Yuck. My very own coding WTF. ) Any help appreciated, Liam Clarke _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor