Reputation: 1536
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?
Upvotes: 0
Views: 70
Reputation: 1536
Eventually fixed it thus:
with open(filename, 'r') as infile:
new_filename = file+'_table.txt'
with open(new_filename, 'w') as outfile:
lines = infile.readlines()
outfile.write(r'\begin{tabular}{|c|c|c|c|}')
for line in lines[1:-3]:
if line.startswith('='):
pass
else:
line = line.replace('\n', ' '+r'\\'+'\n')
outfile.write(line)
outfile.write(r'\hline' + '\n')
outfile.write(r'\end{tabular}')
for dealing with the details of my file.
Upvotes: 0
Reputation: 113
In .txt
file actually there is a \n
in the end of the sentence!
in order to add something at the end of the sentence, we should be careful to this.
I think you can add another line in "for line in lines" to solve this!
line = line.replace("\n", " ")
end_of_line = r'\\'
... and follow
if you want to create another line just use:
outfile.write('\n')
outfile.write(r'\hline')
outfile.write('\n')
I help this can be useful for you.
Upvotes: 1