python - 从 python 脚本 : "TypeError: a float is required" 导出一个 tex 文件文件

标签 python string python-3.x latex

这是写在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)d' 中的 '%(index)s'(以及 'index':3 中的 'index':str(3)),但我仍然遇到相同的错误。 谢谢

最佳答案

您的文本包含另一个 % 字符(在第三行)。由于 % 之后的第一个非空格字符是 f,因此它被解释为 %f(即 float 格式)。您想要通过加倍 (%%) 来避免 % 的出现,或者使用 format 方法而不是 % 运营商。

关于python - 从 python 脚本 : "TypeError: a float is required" 导出一个 tex 文件文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44307828/

相关文章:

python - 如何使用 Pandas 获取每个组中的上一个和中间日期

python - 在 Python 中检索值的字典的嵌套字典列表

java - 从文件中读取 JSON 数据以在 StringEntity 中使用

python - python3 open "x"模式有什么作用?

python - 从单个父脚本运行多个 Python 脚本

python - 正则表达式在 Python 中添加引号,这样我就可以返回 Python 字典

python - Google App Engine 数据存储区查询返回过时数据

python - 安装IB API时遇到错误 "IB API required"

javascript - 在 JavaScript 中连接字符串的最有效方法是什么?

java - Java 字符串中的字符替换