TypeError: format string
先来看一段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的更多相关文章
- Python TypeError: not enough arguments for format string
今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_F ...
- 使用Python过程出现的细节问题:TypeError: not enough arguments for format string
今天使用字符串格式化时,遇到的一点小问题:调用这个方法解释器出错了:TypeError: not enough arguments for format string def ll(name,age) ...
- 关于Python json解析过程遇到的TypeError: expected string or buffer
关于Python json解析过程遇到的问题:(爬取天气json数据所遇到的问题http://tianqi.2345.com/) part.1 url——http://tianqi.2345.com/ ...
- 【错误】python百分号冲突not enough arguments for format string
query = "SELECT * FROM devices WHERE devices.`id` LIKE '%{}%'".format("f2333") d ...
- 解决:error: Cannot fetch repo (TypeError: expected string or buffer)
同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...
- 再探Java基础——String.format(String format, Object… args)的使用
最近看到类似这样的一些代码:String.format("参数%s不能为空", "birthday"); 以前还没用过这功能不知咐意思,后研究了一下,详细讲解如 ...
- OD: Format String, SQL Injection, XSS
Format String 格式化串漏洞 考虑如下的代码: #include<stdio.h> int main() { int a=44,b=77; printf("a=%d, ...
- String.Format(string, arg0)中sring格式
复合格式字符串和对象列表将用作支持复合格式设置功能的方法的参数.复合格式字符串由零个或多个固定文本段与一个或多个格式项混和组成.固定文本是所选择的任何字符串,并且每个格式项对应于列表中的一个对象或装箱 ...
- Oracle问题之literal does not match format string
问题: oerr ora 186101861, 00000, "literal does not match format string"// *Cause: Literals i ...
随机推荐
- linux 系统下安装多个php版本
思路: 下载不同的php源码包,解压后安装在不同的目录下,修改php-fpm监听的端口号 php安装配置参数: ./configure --prefix=/usr/local/php71 --exec ...
- Python读写txt文件时的编码问题
这个问题来自于一个小伙伴,他在处理中文数据时需要先把里面的文本过滤然后分词,因为里面有许多符号,不仅是中文标点符号,还有✳,emoji等奇怪的符号. 正常情况下,中文的str经过encode('utf ...
- 对java中路径的一些理解
开始前先贴一下项目结构 public class TestLocation { @Test public void test1(){ String s1 = Objects.requireNonNul ...
- webpack dllPlugin使用(基于vue-cli webpack模板)
由于本例单入口时打包的文件体积过大,将其分成多入口. 主要涉及到的几个文件为: /index.html, /webpack.dll.config.js, /build/webpack.base.con ...
- 【官方下载】EasyCMDB官方基础版免费下载使用!
链接
- LeetCode--026--删除排序数组中的重复项(java)
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...
- sql自建用户
1.删除数据库中的自建用户:2.在sql中"安全性","登录名",新建个登录名,名称是用户名,采用sql身份验证,去掉密码策略, 选择页下选择“用户映射”,选择 ...
- CF #552(div3)G 最小lcm
题目链接:http://codeforces.com/contest/1154/problem/G 题意:lcm是最小公倍数,本题就是给你一个数组(可能会重复),要求你判断出那两个数的最小公倍数最小, ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 30633: illegal multibyte sequence
import urllib.request def load_baidu(): url = "https://www.baidu.com/" header = {"Use ...
- sqlserver isnull函数
isnull(参数1,参数2),判断参数1是否为NULL,如果是,返回参数2,否则返回参数1. select ISNULL(null,'helloword') 返回helloword字符串select ...