Parsing an unknown data structure in python

I have a file containing a lot of data placed in a form similar to this:

Group1 {  
    Entry1 {  
        Title1 [{Data1:Member1, Data2:Member2}]  
        Title2 [{Data3:Member3, Data4:Member4}]  
    }  
    Entry2 {  
        ...  
    }  
}
Group2 {
    DifferentEntry1 {
        DiffTitle1 {
            ...
        }
    }
}

The fact is, I don’t know how many layers of parentheses there are and how the data is structured. I need to change the data and delete the entire record depending on the conditions associated with the data members before writing everything to a new file. What is the best way to read in such a file? Thanks!

+3
source share
6 answers

I have something similar, but written in java. It parses a file with the same basic structure with slightly different syntax (no '{' and '}' only indented, as in python). This is a very simple script language.

: ( ) . , , . , .

, , google (lizzard-entertainment, 405). , .

  • block_expected, , (, ..). , "{".
  • . .
  • indent_level , . '{}' singns.

BufferedReader input = null;
try {
    input = new BufferedReader(new FileReader(inputFileName));
    // Stack of instruction blocks
    Stack<Block> stack = new Stack<Block>();
    // Push the root block
    stack.push(this.topLevelBlock);
    String line = null;
    Instruction prev = new Noop();
    while ((line = input.readLine()) != null) {
        // Difference between the indentation of the previous and this line
        // You do not need this you will be using {} to specify block boundaries
        int level = indent_level(line) - stack.size();
        // Parse the line (returns an instruction object)
        Instruction inst = Instruction.parse(line.trim().split(" +"));
        // If the previous instruction expects a block (for example repeat)
        if (prev.block_expected()) {
            if (level != 1) {
                // TODO handle error
                continue;
            }
            // Push the previous instruction and add the current instruction
            stack.push((Block)(prev));
            stack.peek().add(inst);
        } else {
            if (level > 0) {
                // TODO handle error
                continue;
            } else if (level < 0) {
                // Pop the stack at the end of blocks
                for (int i = 0; i < -level; ++i)
                    stack.pop();
            }
            stack.peek().add(inst);
        }
        prev = inst;
    }
} finally {
    if (input != null)
        input.close();
}
+1

dict, , , , , , ​​ python,

:

{'group1': {'Entry2': {}, 'Entry1': {'Title1':{'Data4': 'Member4',
'Data1': 'Member1','Data3': 'Member3', 'Data2': 'Member2'}, 
'Title2': {}}}

dict, , , { dict, "Key: Value", , dict, . a}, "" dict, , .

, python, , , , {, }

+3

.

dict_content : NAME ':' NAME [ ',' dict_content ]?
             | NAME '{' [ dict_content ]? '}' [ dict_content ]?
             | NAME '[' [ list_content ]? ']' [ dict_content ]?
             ;

list_content : NAME [ ',' list_content ]?
             | '{' [ dict_content ]? '}' [ ',' list_content ]?
             | '[' [ list_content ]? ']' [ ',' list_content ]?
             ;

dict_content.

, dicts , .

+3

, Python, YAPPS: .

+2

, .

, Python, , , , . .

, , , , , .

, , , .

+1

XML, Python. , - XML, . XML- :

<group id="Group1">  
    <entry id="Entry1">
        <title id="Title1"><data id="Data1">Member1</data> <data id="Data2">Member2</data></title>
        <title id="Title2"><data id="Data3">Member3</data> <data id="Data4">Member4</data></title>
    </entry>  
    <entry id="Entry2">  
        ...
    </entry>
</group>

, XML, , . , XML, . , XML Python . ( , XML, , )

+1

Source: https://habr.com/ru/post/1711366/


All Articles