我正在尝试在Python 2.7.3中编写一个脚本,该脚本可以从Excel电子表格中获取.csv文件并将其转换为适合LaTeX表格的格式。所以我想读取一个文件,并将数据写入一个新的文本文件,但用逗号替换任何逗号,并在每行的末尾附加一个双反斜杠。
例:
输入
A1,A2,A3
B1,B2,B3
C1,C2,C3
期望输出
A1 & A2 & A3 \\
B1 & B2 & B3 \\
C1 & C2 & C3 \\
这就是我现在所拥有的:
old_file = open(selected_file, "r")
new_file = open("texified_" + selected_file.replace("csv","txt"), "w")
#Creates new file with format texified_selected_file.txt
for line in old_file:
new_file.write(line.replace(",", " & ") + r" \\")
new_file.close()
old_file.close()
现在它正确地用符号替换逗号,但不添加双反斜杠。我认为这是因为反斜杠具有特殊含义,但即使将其作为原始字符串,它仍然无效。但它确实将它添加到最后一行的末尾。
实际输出
A1 & A2 & A3
B1 & B2 & B3
C1 & C2 & C3 \\
答案 0 :(得分:1)
发生这种情况可能是因为文件中每行末尾都有newline
,而不是last line
的末尾。
您可以在添加//
之前尝试剥离它,然后单独添加换行符: -
import os
ls = os.linesep
for line in old_file:
new_file.write(line.replace(",", " & ").rstrip() + r' \\ ' + ls)
答案 1 :(得分:0)
我不确定您的代码(或输入数据)是否有问题,但我可能会这样做(可能不那么详细):
for line in old_file:
line = line.strip() # remove newline/whitespace from begin and end of line
line = line.split(',') # get comma-separated values
line = " & ".join(line) # make it ampersand-separated values
line += r" \\" # add latex line break
line += "\n" # add file line break
new_file.write(line)
或者这样:
import jinja2
# define the latex template
template_str = r"""
\documentclass{article}
\begin{document}
\begin{table}
\centering
\begin{tabular}{ccc}
%{ for line in table %} %{{line[0]%}} & %{{line[1]%}} & %{{line[2]%}} \\
%{ endfor %}
\end{tabular}
\end{table}
\end{document}
"""
# initialize the rendering engine
renderer = jinja2.Environment(
block_start_string = '%{',
block_end_string = '%}',
variable_start_string = '%{{',
variable_end_string = '%}}'
)
template = renderer.from_string(template_str)
# bring the data array into shape
lines = [line.strip().split(',') for line in old_file]
# generate the tex source code
with open("test.tex", 'w+') as f:
f.write(template.render(table=lines))
另请查看这些资源: