简体   繁体   English

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

[英]Export a tex file file from python script: “TypeError: a float is required”

this is the content to write in a tex file: 这是要写入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}
'''

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. 我尝试更改'%(index)d'中的'%(index)s'(以及'index':3中的'index':str(3)),但我仍然遇到相同的错误。 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). 由于%之后的第一个非空格字符是f ,因此将其解释为%f (即浮点格式)。 You want to either escape this occurence of % by doubling it ( %% ) or use format method instead of % operator. 您想通过加倍( %% )来逃避%的这种情况,或者要使用format方法代替%运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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