这是写在tex文件中的内容:
content=r'''\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs} % for much better looking tables
\usepackage{multirow}
\usepackage{caption}
\captionsetup{labelformat=empty}
\begin{document}
\begin{table}[htbp]\caption{Common Prediction}
\centering
\input{common%(index)s}
\end{table}
\end{document}
'''
我想用整数值替换“索引”。 当我写:
with open(directoryPath + os.sep +'commonTables3.tex','w') as f:
f.write(content%({'index':str(3)}))
我有以下错误:
f.write(content%({'index':str(3)}))
TypeError: a float is required
我不明白我的错误在哪里。我尝试更改 '%(index)d' 中的 '%(index)s'(以及 'index':3 中的 'index':str(3)),但我仍然遇到相同的错误。 谢谢
最佳答案
您的文本包含另一个 %
字符(在第三行)。由于 %
之后的第一个非空格字符是 f
,因此它被解释为 %f
(即 float 格式)。您想要通过加倍 (%%
) 来避免 %
的出现,或者使用 format
方法而不是 %
运营商。
关于python - 从 python 脚本 : "TypeError: a float is required" 导出一个 tex 文件文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44307828/