簡體   English   中英

從python腳本導出tex文件文件:“ TypeError:需要浮點數”

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

這是要寫入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 (即浮點格式)。 您想通過加倍( %% )來逃避%的這種情況,或者要使用format方法代替%運算符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM