python - not enough arguments for format string

查看:1465
本文介绍了python - not enough arguments for format string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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%'

解决方案

打两个%符号:

In [1]: '%stest%%' % i
Out[1]: '1test%'

这篇关于python - not enough arguments for format string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆