简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM