从python脚本导出tex文件:"TypeError:需要浮点数"

Giu*_*ora 2 python string latex python-3.x

这是要在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}
'''
Run Code Online (Sandbox Code Playgroud)

我想用积分值替换'index'.我写的时候:

with open(directoryPath + os.sep +'commonTables3.tex','w') as f:
    f.write(content%({'index':str(3)}))
Run Code Online (Sandbox Code Playgroud)

我有以下错误:

f.write(content%({'index':str(3)}))
TypeError: a float is required
Run Code Online (Sandbox Code Playgroud)

我不明白我的错误在哪里.我尝试在'%(index)d'中更改'%(index)s'('index'中的'index':str(3):3)但我仍然有相同的错误.谢谢

Bło*_*tek 6

您的文字包含另一个%字符(在第三行).由于后面的第一个非空格字符%f,它被解释为%f(即浮点格式).你想要%通过加倍(%%)或使用format方法而不是%运算符来逃避这种情况.