2017-06-01 39 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方法而不是%運算符來避免出現%

相關問題