我最近遇到了这个TypeError异常,我发现很难调试它.我最终将其简化为一个小测试用例:
>>> "{:20}".format(b"hi")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: non-empty format string passed to object.__format__
对我来说,这是非常不明显的.我的代码的解决方法是将字节字符串解码为unicode:
>>> "{:20}".format(b"hi".decode("ascii"))
'hi '
这个例外是什么意思?有没有办法让事情变得更清楚?