1

当我尝试使用此命令时

appuifw.note(u"Connecting to %s" % (address), "conf");

我收到错误:“并非所有参数都在字符串格式化期间转换”

如何修复此错误?

4

2 回答 2

1

字典还有另一种选择

mydict = {'foo': 'bar', 'foo2': 'bar2'}
print "my name is %(foo)s" % mydict

这样,您可以根据需要仅使用 foo 。

注意最后一个 's' 代表 'string',所以如果你添加类似 %(foo)d 的东西,它意味着 %d 的名字是 foo。

于 2012-02-16T14:08:23.857 回答
1

您提到的错误出现在以下情况

>>> print "my name is %s" %('foo','bar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

这意味着我有 2 个值要替换('foo', 'bar'),但只提供了oneplace holder (%s)

纠正它

>>> print "my name is %s %s" %('foo','bar')
    my name is foo bar

还有另一种使用str.format()实现此目的的方法。

>>> print "my name is {0} {1}".format('foo','bar')
    my name is foo bar
于 2012-02-16T02:11:13.517 回答