先来看一段Python代码:

class Negate:
def __init__(self, val):
self.val = -val
def __repr__(self):
return str(self.val) if __name__ == '__main__':
print('{0:5}'.format(-5)) x = Negate(5)
print(x)
print('{0:5}'.format(x))

这段代码在不同的Python版本下有不同的输出结果:

在Python 2.7中,输出为:

-5

-5

-5

在Python 3.4中,输出为:

-5

-5

TypeError: non-empty format string passed to object.__format__

在Python 3.6中,输出为:

-5

-5

TypeError: unsupported format string passed to Negate.__format__

从上面的输出中可以看出,使用format来对齐数字和字符串时,其效果是不同的:

>>> '{0:5}'.format(-5)

'   -5'

>>> '{0:5}'.format('-5')

'-5   '

另外,对于Python3.4和Python 3.6中输出的TypeError错误,原因如下:

变量名x所引用的对象的类型为<class '__main__.Negate'>,而该类型没有自己的__format__()方法,所以只能使用默认继承自object对象的__format__()方法,而该默认方法不支持任何格式化选项(比如字段宽度),所以会引发上述TypeError错误。

对于上述TypeError错误,解决方法就是先把对象转换为字符串。转换方式有两种:

方式一:

print('{0:5}'.format(str(x)))

方式二:

print('{0!s:5}'.format(x))

使用其中的任何一种方式都可以。

TypeError: format string的更多相关文章

  1. Python TypeError: not enough arguments for format string

    今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_F ...

  2. 使用Python过程出现的细节问题:TypeError: not enough arguments for format string

    今天使用字符串格式化时,遇到的一点小问题:调用这个方法解释器出错了:TypeError: not enough arguments for format string def ll(name,age) ...

  3. 关于Python json解析过程遇到的TypeError: expected string or buffer

    关于Python json解析过程遇到的问题:(爬取天气json数据所遇到的问题http://tianqi.2345.com/) part.1 url——http://tianqi.2345.com/ ...

  4. 【错误】python百分号冲突not enough arguments for format string

    query = "SELECT * FROM devices WHERE devices.`id` LIKE '%{}%'".format("f2333") d ...

  5. 解决:error: Cannot fetch repo (TypeError: expected string or buffer)

    同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...

  6. 再探Java基础——String.format(String format, Object… args)的使用

    最近看到类似这样的一些代码:String.format("参数%s不能为空", "birthday"); 以前还没用过这功能不知咐意思,后研究了一下,详细讲解如 ...

  7. OD: Format String, SQL Injection, XSS

    Format String 格式化串漏洞 考虑如下的代码: #include<stdio.h> int main() { int a=44,b=77; printf("a=%d, ...

  8. String.Format(string, arg0)中sring格式

    复合格式字符串和对象列表将用作支持复合格式设置功能的方法的参数.复合格式字符串由零个或多个固定文本段与一个或多个格式项混和组成.固定文本是所选择的任何字符串,并且每个格式项对应于列表中的一个对象或装箱 ...

  9. Oracle问题之literal does not match format string

    问题: oerr ora 186101861, 00000, "literal does not match format string"// *Cause: Literals i ...

随机推荐

  1. Left Join on 多条件查询时,条件过滤的问题

    例如:A  Left Join B on (...) on 后面的条件是对B数据的过滤,如果要对A的数据或者联合之后的数据集进行过滤,则要把过滤条件放在where子句中

  2. Shopping List

    剃须啫喱 吉列 70g $22.00 鲁阳家居专营店 剃须水套装 阿帕奇+酷曼 230g+230g $16.80(限时活动) 鲁阳家居专营店 鸡胸肉 撸铁党 100gx9+100g $49.9(限时活 ...

  3. sysbench工具安装使用

    一.sysbench简介 Sysbench是一款开源的.跨平台的.模块化的.多线程的性能测试工具,通过高负载地运行在数据库上,可以执行CPU.内存.线程.IO.数据库等方面的性能测试.用于评估操作系统 ...

  4. DPDK kni创建要先于port开启

    DPDK kni创建要先于port开启 1. DPDK kni创建使用API:- rte_kni_init- rte_kni_alloc 2. DPDK port开启使用API:- rte_eth_d ...

  5. D - Dice Game (BFS)

    A dice is a small cube, with each side having a different number of spots on it, ranging from 1 to 6 ...

  6. Spring普通类/工具类获取并调用Spring service对象的方法

    参考<Spring普通类获取并调用Spring service方法>,网址:https://blog.csdn.net/jiayi_0803/article/details/6892455 ...

  7. idea使用eclipse 代码format风格

    为了保证大家代码格式一样,避免在合代码时很出现大面积冲突.针对eclipse和idea两种开发工具进行了代码风格统一. Eclipse 使用方法:Windows → Preferences → Jav ...

  8. JAVA写接口傻瓜(#)教程(四)

    接上篇 7.sevlert 啊啊啊终于写到最重要的实现部分了.Servlet = Service + Applet,表示小服务程序.Servlet 是在服务器上运行的小程序.这个词是在 Java ap ...

  9. leetcode 78,236,300

    ---恢复内容开始--- 2018.3.16目前已刷27题,打卡记录有意思的题目. leetcode78 subsets 思路1:DFS遍历子集,每遇到一个数就把该数加上原来的子集变成新的子集. cl ...

  10. A锚点实现,滚动页面内容改变tab选项

    Css: ul{margin:0;padding:0;list-style:none;} a{ text-decoration: none; outline:none; -webkit-tap-hig ...