sado
sado

Reputation: 23

latex tables from python output

I am new to Python and have started doing some calculations using anaconda/spyder enviroment. I use the python 2.6 as i think it has greater compatibility with programs.

I am determining some values in Python and want the values to be saved in a table in a text file. Ones the text file is output i will copy the table to my latex document. The code looks like this

import numpy as np
# Input values
BC = "g" # Input p=Poor bond or g = Good bond

if BC == "p":
n1 = 0.7
print n1
elif BC=="g":
n1 = 1
print n1
else:
print "Missing inddata "

np.savetxt('Output.txt',['\\'"begin{table}[ht]" '\n' 
                      '\\'"centering" '\n'
                      '\\'"begin{tabular}{l|c|c|c|c}" '\n'
                      '\\'"hline"'\\'"hline"  '\n'
                      '\\'"textbf{Number of strings} & $" '\\'"beta_{1}$ & $" '\\' "beta_{2}$ & $" '\\' "beta_{3}$ & $" '\\'  "beta_{4}$"   "\\\\" "[1ex]" '\n'
                      '\\'"hline"  '\n'
                      "Two &" n1  "& 75  &" '\\' "cellcolor[gray]{.4} &" '\\' "cellcolor[gray]{.4}"  "\\\\"], fmt='%s')

n1 is the value i have calculated and which is among the strings printed to the text file, but this is not working, perhaps because the format is string. I dont know how to get this to work or whether there is a smarter way of creating beautiful latex tables.

Thanks in advance.

Upvotes: 2

Views: 2114

Answers (1)

yacc
yacc

Reputation: 3361

You need to convert the float into a string. Just concatenating it with a string (like "a" + n1 + "b" will not work, you need to convert explicitly. The easiest way is to use str(n1) like so:

"Two &" + str(n1) + "& 75  &"

Upvotes: 1

Related Questions