先来看一段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. Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com.test.bean.groupMapper is not known to the MapperRegistry.

    Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com. ...

  2. php rsa

    <?php $res=openssl_pkey_new(); // Get private key $ok = openssl_pkey_export($res, $privkey); // G ...

  3. java知识点集锦--基础知识部分

    1.面向对象特征:封装.继承.多态 封装:把数据和操作数据的方法绑定起来,对数据的访问只能通过已定义的接口来访问.面向对象的本质就是将现实世界描绘成一系列完全自治.封闭的对象.我们在类中编写的方法就是 ...

  4. Bubble sort of sorting algorithm

    Bubble sort,It's a relatively basic algorithm.The core implementation ideas are as follows: 1.Define ...

  5. CHM文件命令转换成html

    在Windows下chm转换为html的超简单方法(反编译CHM文件的方法) 通过调用Windows命令,将chm 文件转换为html 文件. 方法: 命令行(cmd),输入hh -decompile ...

  6. OpenBUGS抽样数据基本操作

    新建两个文件 第一个文件写操作程序,第二个文件写数据数量,类似于下图 鼠标聚焦在第一个文件中,点击工具栏的Model,选择Specification,然后点击check Moel,会有弹框出现 点击确 ...

  7. GIS开发 图形常见算法

    摘录:OSGeo中国中心 http://www.osgeo.cn/post/ae457 计算机的出现使得很多原本十分繁琐的工作得以大幅度简化,但是也有一些在人们直观看来很容易的问题却需要拿出一套并不简 ...

  8. 【Java】【12】精确的加减乘除运算

    前言:用了BigDecimal对象 正文: 1,加法 /** * @param v1 被加数 * @param v2 加数 * @param scale 保留几位小数*/ public static ...

  9. Vue2全家桶之二:vue-router(路由)详细教程,看这个就够了

     作者:东西里本文转载于:https://www.jianshu.com/p/514c7588e877来源:简书 转载仅供自己日后看方便.  由于Vue在开发时对路由支持的不足,于是官方补充了vue- ...

  10. js中Array数组基本方法

    总结:push() 添加元素到数组未尾, 返回数组长度 unshift() 添加元素到数组头部, 返回数组长度 pop() 删除数组未尾元素, 返回删除元素 shift() 删除数组头部元素, 返回删 ...