这是输出。我相信这些是 utf-8 字符串......其中一些可以是 NoneType 但它会立即失败,在这样的字符串之前......
instr = "'%s', '%s', '%d', '%s', '%s', '%s', '%s'" % softname, procversion, int(percent), exe, description, company, procurl
TypeError:格式字符串的参数不足
7换7吗?
您需要将格式参数放入一个元组(添加括号):
instr = "'%s', '%s', '%d', '%s', '%s', '%s', '%s'" % (softname, procversion, int(percent), exe, description, company, procurl)
您目前拥有的相当于以下内容:
intstr = ("'%s', '%s', '%d', '%s', '%s', '%s', '%s'" % softname), procversion, int(percent), exe, description, company, procurl
例子:
>>> "%s %s" % 'hello', 'world'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> "%s %s" % ('hello', 'world')
'hello world'
请注意,%
格式化字符串的语法已经过时了。如果你的 Python 版本支持它,你应该写:
instr = "'{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}'".format(softname, procversion, int(percent), exe, description, company, procurl)
这也解决了您碰巧遇到的错误。
%
在我的格式字符串中用作百分比字符时,我遇到了同样的错误。解决这个问题的方法是加倍%%
。
我有同样的问题,我出于特定原因使用原始查询,这是在 TIME_FORMAT 函数中添加双引号。
User.objects.raw(
f'SELECT f1,f2,TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(end_time) - TIME_TO_SEC(start_time))),"%%H:%%i") AS log FROM users GROUP BY start_dt')