I have a large number of long text files that I want to convert into LaTex-formatted tables. Here's a short example of one of the files:
List of the best combinations (with |r-value| > 0.5)
Combination & r value & no.obs. & Kendall's tau
============================================================
B - V & 0.580019 & 11863 & 1.000000
B - R & 0.574867 & 11863 & 1.000000
V - B & -0.580019 & 11863 & 1.000000
R - B & -0.574867 & 11863 & 1.000000
Highest r-value of 0.580019 occurred for B - V
Lowest r-value of -0.580019 occurred for V - B
I need to convert this into a table in my LaTex document, so it needs to be formatted like:
List of the best combinations (with |r-value| > 0.5)\\
\hline
Combination & r value & no.obs. & Kendall's tau\\
============================================================\\
B - V & 0.580019 & 11863 & 1.000000\\
\hline
B - R & 0.574867 & 11863 & 1.000000\\
\hline
V - B & -0.580019 & 11863 & 1.000000\\
\hline
R - B & -0.574867 & 11863 & 1.000000\\
\hline
Highest r-value of 0.580019 occurred for B - V\\
Lowest r-value of -0.580019 occurred for V - B\\
The real files will be dozens of lines long, so it will be impractical to do it by hand.
I've tried
filename = file+'.txt'
with open(filename, 'r') as infile:
new_filename = file+'_table.txt'
with open(new_filename, 'w') as outfile:
lines = infile.readlines()
for line in lines:
end_of_line = r'\\'
outfile.write(line + end_of_line)
outfile.write(r'\\hline')
as well as the suggestions from here, but my output is
\List of the best combinations (with |r-value| > 0.5)
\Combination & r value & no.obs. & Kendall's tau
\============================================================
\B - V & 0.580019 & 11863 & 1.000000
\B - R & 0.574867 & 11863 & 1.000000
\V - B & -0.580019 & 11863 & 1.000000
\R - B & -0.574867 & 11863 & 1.000000
\
\Highest r-value of 0.580019 occurred for B - V
\Lowest r-value of -0.580019 occurred for V - B
\
\
How can I insert \\
and \hline
into the outfile
verbatim? Or is there any other tool I could use to convert to LaTex formatting?