使用表格Python packag生成适当的乳胶表

2024-04-29 06:35:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用tabulatePython包生成一个正确的乳胶格式的表。

这是一个MWE:

from tabulate import tabulate

table = [[r"${:.1f}\pm{:.1f}$".format(2.3564, 0.5487)],
         [r"${:.1f}\pm{:.1f}$".format(45.1236, 8.00021)]
         ]

print tabulate(table, tablefmt="latex")

我从这个例子中得到的是:

\begin{tabular}{l}
\hline
 \$2.4\textbackslash{}pm0.5\$  \\
 \$45.1\textbackslash{}pm8.0\$ \\
\hline
\end{tabular}

当正确的格式是:

\begin{tabular}{l}
\hline
 $2.4\pm0.5$  \\
 $45.1\pm8.0$ \\
\hline
\end{tabular}

即:在$符号之前插入反斜杠,并用\textbackslash{}替换\pm中的间隙。

是否可以生成正确的格式化表?


Tags: fromimportformat格式table乳胶endtabulate