python格式化文本,Python的格式化文本

2a0ec7faf851

python.png

文:张利东

公众号:DongTalks

1. 缘起

程序处理无非输入、处理和输出,其中输出以文本居多,文本的格式化是绕不过去的问题,不如索性好好研究一下搞定,省的每次遇到都尴尬。

2. 探索

根据PEP 3101,Python有两种典型的文本格式化方法

The '%' operator for strings.

The string.Template module.

其中%的方法和C语言printf的格式化很像,简单实用,但在Python里面有很多问题,比如处理tuples 和 dictionaries时候就容易出错。string.Template用的是类似的Format String Syntax语法,很好的解决了这些问题,而且功能更强大。但强大也带来了复杂性,Format语法比%要复杂的多。

2.1. % Style

%风格来自C语言的printf,但也有一些不同。标准的C语言格式为

%[parameter][flags][width][.precision][length]type

Python去掉了parameter参数,在其位置增加了Mapping key。根据Python文档,其关键语法为

The '%' character, which marks the start of the specifier.

Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)).

Conversion flags (optional), which affect the result of some conversion types.

Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.

Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual precision is read from the next element of the tuple in values, and the value to convert comes after the precision.

Length modifier (optional).

Conversion type.

Maping Key主要是为了解决类似Dict一类参数的调用问题。但这种解决方式不太优雅,这种复杂格式还是用后面谈到的Format Syntax为妙。

>>> p = 'This is a test for %012.2f.' % 100.123

>>> p

'This is a test for 000000100.12.'

其中

%表示格式化开始;

Mapping参数省略;

0为flag,代表占位符,位数不足时用它填充;

12为小数点前面最小长度,不足处用flag填充;

.2表示小数点后面精确位数;

f表示浮点数类型;

2.2. Format Style

Format Sytle是Python在2.6版本里面引入的一种文本格式化的方法。其主要格式为

"string content {[replacement_field]}".format(content)

其中,replacement_field 的典型格式为 "{" [field_name] ["!" conversion] [":" format_spec] "}"。

field_name是变量标记,!和:是分隔符,分别区分conversion和format_spec。

conversion有三种方法,"r" | "s" | "a"。虽然文档没有明说,但想来默认应该是"s"吧。

format_spec内容比较复杂,也是这种方法强大所在。其语法结构为

[[fill]align][sign][#][0][width][grouping_option][.precision][type]

其中

fill表示填充;

align表示对齐;

sign表示正负号;

#表示进制选择;

0没搞明白是什么;

width表示文字宽度;

grouping_option表示千分位开关;

precision表示小数点后精度;

type表示类型;

其中type可以参考% Style,大同小异;也可以参考官方文档。其典型应用有如下代码

>>> s = 'This is a test for {:$^+016,.2f}.'.format(100.123)

>>> s

'This is a test for $$$$+100.12$$$$$.'

这个0还是没有搞清楚是干嘛的,难不成是打酱油的……

3. 总结

今天我们大致总结了Python里面文本输出的格式问题。国庆最后几天搞定了这件事情,挺开心。

这几天看王兴的微博,里面有段话挺让人触动

如果我一整天都没看到、想到、或做过什么值得在饭否上说的事,那这一天就太浑浑噩噩了。

日有所进,说起来容易,做起来其实很难。反思了一下自己,真不是每天都能吹吹牛。所以决定开启一个「日拱一卒」的计划,争取每天都能有点收获,有点能吹吹牛的事情。

今天么,就是大致搞明白了Python的这个格式化输出。当然,一如以往,还有一些没有搞明白,比如那个0……囧。

你呢?

4. 参考文献:

2a0ec7faf851

2017-08-29-DongTalks.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值