2

I'm trying to process tens of tables (from multiple org-files) in python src-block at once.

Suppose that the following file: /tmp/file1.org contains some tables:

* This org contains the tables
#+name: table_example_1
| 1 | 2 | 3 |
| 4 | 5 | 5 |

#+name: table_example_2
| a | b | c |
| d | e | f |

#+name: table_example_3
| a1 | b2 | c3 |
| d4 | e5 | f6 |

AFAIK (to use all tables from previous org-file), I have the following two methods:

* method 1
#+BEGIN_SRC python :results output :var tbl1=/tmp/file1.org:table_example_1 tbl2=/tmp/file1.org:table_example_2 tbl3=/tmp/file1.org:table_example_3
print(tbl1)
print(tbl2)
print(tbl3)
#+END_SRC

#+RESULTS:
: [[1, 2, 3], [4, 5, 5]]
: [['a', 'b', 'c'], ['d', 'e', 'f']]
: [['a1', 'b2', 'c3'], ['d4', 'e5', 'f6']]

-----------------------------------------------------------------------------------------------------------------------------------------------
* method 2
:PROPERTIES:
:header-args: :var tbl1=/tmp/file1.org:table_example_1 tbl2=/tmp/file1.org:table_example_2 tbl3=/tmp/file1.org:table_example_3
:END:

#+BEGIN_SRC python :results output 
print(">", tbl1)
print(">", tbl2)
print(">", tbl3)
#+END_SRC

#+RESULTS:
: > [[1, 2, 3], [4, 5, 5]]
: > [['a', 'b', 'c'], ['d', 'e', 'f']]
: > [['a1', 'b2', 'c3'], ['d4', 'e5', 'f6']]

Problem: You can see that lines #+BEGIN_SRC python .... and :header-args: ... are very long. Now imagine, there are 20-30 tables. It's quite annoying to manage this long line.


Question:

  • Is it possible to divide these too long header-arguments into multiple lines to make it easy to manage?
  • Or is there an easier method to achieve what I'm doing?
3
  • 1
    you could also export your tables to csv with org-table-export and read the csv from Python.
    – crocefisso
    Commented Apr 21, 2023 at 20:10
  • That's a good idea, definitely will try it
    – Garid
    Commented Apr 21, 2023 at 20:13
  • It shouldn't be too complicated to build a function to automatically export all the tables in your org file to csv.
    – crocefisso
    Commented Apr 21, 2023 at 20:17

1 Answer 1

2

I just found out the existence of :header-args+:. (from another question).

So following works:

* method 3
:PROPERTIES:
:header-args:  :var table1=/tmp/file1.org:table_example_1 
:header-args+: :var table2=/tmp/file1.org:table_example_2 
:header-args+: :var table3=/tmp/file1.org:table_example_3
:END:

#+BEGIN_SRC python :results output 
print("<", table1)
print("<", table2)
print("<", table3)
#+END_SRC

#+RESULTS:
: < [[1, 2, 3], [4, 5, 5]]
: < [['a', 'b', 'c'], ['d', 'e', 'f']]
: < [['a1', 'b2', 'c3'], ['d4', 'e5', 'f6']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.