我有大量长文本文件,我想转换成LaTex格式的表。以下是其中一个文件的简短示例:
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
我需要将其转换为LaTex文档中的表格,因此需要格式化为:
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\\
真正的文件将长达数十行,因此手动完成是不切实际的。
我试过了
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')
以及来自here的建议,但我的输出是
\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
\
\
如何将\\
和\hline
插入outfile
逐字?或者是否有任何其他工具可用于转换为LaTex格式?
在.txt
文件中,实际上句子末尾有一个\n
!为了在句末添加内容,我们应该小心这一点。
我想你可以在“for line in lines”中添加另一行来解决这个问题!
line = line.replace("\n", " ")
end_of_line = r'\\'
......然后跟随
如果你想创建另一行,只需使用:
outfile.write('\n')
outfile.write(r'\hline')
outfile.write('\n')
我帮助这对你有用。
最终修复它:
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}')
处理我的文件的详细信息。