Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a LaTex table into an array in Python

I have a difficult mission for a beginner in Python, I need to import a table from a source file which is written in LaTex. I was thinking I will use the name of the table as identifier, and then write line by line into an array, from the beginning of the table to its end. What is the "natural" way to do this job?

like image 860
Ohm Avatar asked Feb 18 '13 16:02

Ohm


1 Answers

The astropy package has a LaTeX table reader.

from astropy.table import Table
tab = Table.read('file.tex')

The reader function should automatically recognize the format and read the first table in the file. (Cut and paste the relevant section into a new file if you want a later table). The reader has some limitations, though. Most importantly, every row of data has to be on a single line (The link to the table in the questions is dead, so I cannot see if this is a problem) and there cannot be commands like \multicolumn or \multirow.

Check the docs for Latex reading in astropy for more options: https://astropy.readthedocs.org/en/latest/api/astropy.io.ascii.Latex.html#astropy.io.ascii.Latex

like image 142
hamogu Avatar answered Oct 19 '22 03:10

hamogu