i = 1
item_add = ''
content = 'test%'
item_add = ('%s' + '.' + content + '<br>') % i
print item_add
执行上面的程序会报错:TypeError: not enough arguments for format string
只要content中含有%的话,python 认为它是转移符,而实际我需要的就是%
我又不能把%进行替换,因为我需要的就是它。。
大家觉得怎么做比较好呢?
源程序是一段django的代码:
def hostcomment_display(self, obj):
item_add = ''
i = 1
for hostcomment_item in h.hostcomment_set.all():
content = hostcomment_item.comment
item_add += ('%s' + '.' + content + '\<br\>') % i
i += 1
return item_add
如果content中包含%,就报错TypeError: not enough arguments for format string
content = 'software备机--老的备份机-根94%'
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
打两个%符号:
我建议你提问题的时候,代码至少也要使用markdown来写。不然你这问题别人看都很难看清楚代码,就更别指望别人来回答你的问题了。至于markdown怎么写,请自行google。
其实你的问题中:
如果想要在格式化中显示百分号
%, 需要写成%%,因此正确的写法是:已经解决了。做了个判断。