Trying to use docutils.parsers.rst.tableparser in Python
Question: I would like to use the parser in the Python docutils.parsers.rst.tableparser
package to grab a plaintext table and parse it easily. The format of the
tables tableparser can read is very convinient for my project.

The problem is that, even though the documentation says that the input to the
parse(block) function is a 'list of lines of text; no whitespace padding',
whenever I try to parse something it fails.

So a small piece of code like this:

    
    
    import docutils.parsers.rst.tableparser as tbp
    
    parser = tbp.GridTableParser()
    parser.parse(['+---+---+', '| a | b |', '| c | d |', '+---+---+'])
    

will fail with the following error message:

    
    
    File "[...]/python2.7/site-packages/docutils/parsers/rst/tableparser.py", line 149, in setup
       self.block.disconnect()  # don't propagate changes to parent
    
    AttributeError: 'list' object has not attribute 'disconnect'
    

I've been trying to find examples of use of this function online but I haven't
been able to find anything, any clue on what type of 'list of lines of text'
do I need to pass to the `parse()` function?


Answer: After some source browsing I got to this point, it appears to be working :)

    
    
    from docutils.parsers.rst import tableparser
    from docutils import statemachine
    
    parser = tableparser.GridTableParser()
    block = statemachine.StringList([
        '+---+---+',
        '| a | b |',
        '| c | d |',
        '+---+---+',
    ])
    print parser.parse(block)
    

Result:

    
    
    (
        [3, 3],
        [],
        [[
            (0, 0, 1, StringList(['a', 'c'], items=[(None, 1), (None, 2)])),
            (0, 0, 1, StringList(['b', 'd'], items=[(None, 1), (None, 2)])),
        ]],
    )
    

