2017-06-01 62 views
2

这是一个TEX文件中写入内容:我想用integger值来代替“指数”出口从Python脚本一个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'中更改'%(index)d'(和'index':str(3)中的'%(index)s':3)但我仍然有相同的错误。 谢谢

回答

6

您的文字还有另一个%字符(在第三行)。由于%之后的第一个非空格字符是f,因此它被解释为%f(即浮点格式)。您希望通过将其加倍(%%)或使用format方法而不是%运算符来避免出现%

相关问题