I'm trying to write some python code that reads specified tables in org mode documents. For example, I have a file ~/foo.org$ cat ~/foo.org#+Title: Example Org Document* Section OneProin quam nisl, tincidunt et, mattis eget, convallis nec, purus.#+TBLNAME: table1| i     | want    | python | to     ||-------+---------+--------+--------|| read  | this    | table  | only   || 1     | 3       | 2      | 4      ||-------+---------+--------+--------|| i     | want    | the    | dashed || lines | ignored | by     | python |#+TBLNAME: table2|    i | don't |  need | python | to ||------+-------+-------+--------+----|| read |  this | table |      9 |  8 ||    7 |     6 |     5 |      4 |  3 ||    2 |     1 |     0 |     22 | 17 |If my file were less complex, say$ cat ~/bar.org| i     | want    | python | to     || read  | this    | table  | only   || 1     | 3       | 2      | 4      || i     | want    | the    | dashed || lines | ignored | by     | python |then I could read the table into python with import csvcsv.DictReader(open('~/bar.org'), delimiter='|')Is there any way to parse my desired table from the more complicated ~/foo.org?
Can python extract org-mode tables from org documents?
python;csv;org mode
The easiest thing to do if you control the input format you expect is to read the entire file into python, extract with a regular expression the table of interest, then pass this to csv:import csv, redata = open(foo.org).read()mo = re.search(TBLNAME: table1\n.*?\n(\n|$), data, re.S)mytable = mo.group(0)d = csv.DictReader(mytable.splitlines(), delimiter='|')You need to convert the string mytable into an iterable for the csv reader, eg as shown here by splitting out the lines.