gpt4 book ai didi

Python 类型错误 : not enough arguments for format string in case of string %s

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:07 26 4
gpt4 key购买 nike

wrapper = """<html>
<head>
</head>
<body>
<table style="height: 100px;" width="500">
<tbody>
<tr>
<td rowspan="3"><a id="imgrec1" href="http://www.w3.org"><img src="%s" alt="Smiley face" width="50" height="100" /><br /><br /></a></td>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
</tr>
<tr>
<tr>
<td rowspan="3"><a id="imgrec1" href="http://www.w3.org"><img src="%s" alt="Smiley face" width="50" height="100" /><br /><br /></a></td>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
</tr>
<tr>
<td>%s</td>
</tr>
<tr>
</tbody>
</table>
</body>
</html>"""

str=""
for i in reclist[:2]:
str=str + "I[\"" + i + "\"]," + "T[\"" + i + "\"]," +"R[\"" + i + "\"]," +"P[\"" + i + "\"],"
str = str[:-1]
print str
whole = wrapper % (str)

Output: I["M1"],T["M1"],R["M1"],P["M1"],I["M2"],T["M2"],R["M2"],P["M2"]
whole = wrapper % (str)
TypeError: not enough arguments for format string

因为 wrapper 恰好有 8 个 %s 而 str 也有 8 个元组,但仍然显示错误 TypeError: not enough arguments for format string.

但是如果我不使用 str 而不是

    whole = wrapper %  
(I["M1"],T["M1"],R["M1"],P["M1"],I["M2"],T["M2"],R["M2"],P["M2"])

然后它工作正常。

我指的是 stackoverflow stackoverflow对于这个问题,但它没有帮助。

最佳答案

我想你会得到一个错误,因为你在格式化字符串时将字符串作为单个参数传递。您可以先创建一个列表:

str = []
for i in reclist[:2]:
str += [I[i], T[i] etc.]

然后使用:

whole = wrapper % tuple(str)

这样列表中的每个项目都将作为单独的参数传递。

关于Python 类型错误 : not enough arguments for format string in case of string %s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com