Parsing an unknown data structure in python -


I have a file containing several types of data:

  Group1 {Entry1 {title1 [{Data1: Member1, Data2: Member2}] Heading 2 [{Data3: Member3, Data4: Member4}}} Entry2 {...}} Group 2 {Separate Intent 1 {DiffTitle1 {...}} }  

The thing is, I do not know how many layers of brackets are, and how the data is structured. I need to modify the data, and before writing everything in a new file, delete the entire entry based on the conditions related to the data entries. What is the best way to read this in a file like this? Thanks!

I have something similar but have been written in Java. It parses a file with a slightly different syntax (a '{' and '}' in a dragon just like a dragon) with the same basic structure. This is a very simple script language.

Actually it works like this: It uses a stack to keep track of the block (or your case data) within the instructions and adds new directions to the block. up. If it parses an instruction that expects a new block then it is pushed onto the stack. If a block ends, an element pops from the stack.

I do not want to post the entire source because it is large and is available on Google code (lizzard-entertainment, revision 405). There are some things you should know about.

  • The instruction is an abstract class and its block_appointment method requires that a concrete instruction needs a block (like loop, etc.) in your case is unnecessary, you only check for '{'} Need to
  • Increases block direction. There is a list of instructions in this and there is an add method to add more.
  • How many places are before the instruction lesson to return to indent_level, it is also unnecessary with '{}' singing.

placeholder

  BufferedReader input = null; Try {input = new BufferedReader (New FileReader (inputFileName)); // Stack of instruction blocks stack & lt; Block & gt; Stack = new stack & lt; Block & gt; (); // Root block stack.push push (this.topLevelBlock); String line = null; Instructions prev = new nop (); Whereas ((line = input.readLine ())! = Null {// The difference between indentation of the previous and this line // You do not need that you will use {} to specify block boundaries int level = Indent_level (line) - stack size (); // Parse line (gives a command object) Inst Instruction.parse (line.trim (.) Split ("+"); // If the previous instruction is expected to repeat a block (for example) (prev.block_expected ()) {if (level! = 1) {// TODO handling error continue; } // Push the previous instruction and add the current instructions stack. Add pitch (block) (Previous)); . Adding Stack.peek () (Inst); } Else {if (level> 0) {// TODO handle error is continuing; } And if (level> lt; 0) {// stack pop at the end of blocks (int i = 0; i <-level; ++ i) stack.pop (); } Stack.peek (). Add (inst); } Prev = inst; }} Finally {if (input! = Tap) input.close (); }  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -