Giuseppe Angora
Giuseppe Angora

Reputation: 853

Export a tex file file from python script: "TypeError: a float is required"

this is the content to write in a tex file:

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}
'''

i want to replace 'index' with a integger value. When i write:

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

i have the follow error:

f.write(content%({'index':str(3)}))
TypeError: a float is required

i don't understand where is my mistake. I try to change '%(index)s' in '%(index)d' (and 'index':str(3) in 'index':3) but i still have the same error. thanks

Upvotes: 2

Views: 73

Answers (1)

Błotosmętek
Błotosmętek

Reputation: 12927

Your text contains another % character (in third line). Since the first non-space character after % is f, it is interpreted as %f (ie. float format). You want to either escape this occurence of % by doubling it (%%) or use format method instead of % operator.

Upvotes: 6

Related Questions