【python cookbook】【字符串与文本】16.以固定的列数重新格式化文本
问题:重新格式化一些很长的字符串,以指定的列数来显示
解决方案:textwrap模块的fill()方法来实现
# A long string
s = "Look into my eyes, look into my eyes, the eyes, the eyes, \
the eyes, not around the eyes, don't look around the eyes, \
look into my eyes, you're under." import textwrap print(textwrap.fill(s, 70))
print() print(textwrap.fill(s, 40))
print() print(textwrap.fill(s, 40, initial_indent=' ')) #initial_indent:在第一行的行首添加指定的字符显示。默认是空白。如果首行是空白行就不会添加。print() print(textwrap.fill(s, 40, subsequent_indent=' ')) #subsequent_indent:除了第一行,所有其它行都在行首添加这个字符串的输出。默认为空。
print()
>>> ================================ RESTART ================================
>>>
Look into my eyes, look into my eyes, the eyes, the eyes, the eyes,
not around the eyes, don't look around the eyes, look into my eyes,
you're under. Look into my eyes, look into my eyes,
the eyes, the eyes, the eyes, not around
the eyes, don't look around the eyes,
look into my eyes, you're under. Look into my eyes, look into my
eyes, the eyes, the eyes, the eyes, not
around the eyes, don't look around the
eyes, look into my eyes, you're under. Look into my eyes, look into my eyes,
the eyes, the eyes, the eyes, not
around the eyes, don't look around
the eyes, look into my eyes, you're
under. >>>
关于终端的尺寸大小,可以通过os.get_terminal_size()来获取:
>>> import os
>>> os.get_terminal_size().columns
80
fill()方法还有其他的额外的选项可以用来控制制表符、句号等,请参阅textwrap.TextWrapper类的文档 https://docs.python.org/3.3/library/textwrap.html#textwrap.TextWrapper
【python cookbook】【字符串与文本】16.以固定的列数重新格式化文本的更多相关文章
- python cookbook 字符串和文本
使用多个界定符分隔字符串 import re line = 'asdf fjdk; afed, fjek,asdf, foo' print(re.split(r'[;,\s]\s*', line)) ...
- python书籍推荐:Python Cookbook第三版中文
所属网站分类: 资源下载 > python电子书 作者:熊猫烧香 链接:http://www.pythonheidong.com/blog/article/44/ 来源:python黑洞网 内容 ...
- python 如何将md5转为16字节
python的hashlib库中提供的hexdigest返回长度32的字符串. md5sum是128bit,也就是16字节,如何将python生成字符串的转为16字节呢? 请看下面代码 import ...
- 【python cookbook】【字符串与文本】9.将Unicode文本统一表示为规范形式
问题:确保所有的Unicode字符串都拥有相同的底层 解决方案:为解决同一个文本拥有多种不同的表示形式问题,应该先将文本统一表示为规范形式,这可以通过unicodedata模块来完成, unicode ...
- python常用的十进制、16进制、字符串、字节串之间的转换
进行协议解析时,总是会遇到各种各样的数据转换的问题,从二进制到十进制,从字节串到整数等等 废话不多上,直接上例子 整数之间的进制转换: 10进制转16进制: hex(16) ==> 0x10 ...
- [转]python常用的十进制、16进制、字符串、字节串之间的转换
阅读目录(Content) 整数之间的进制转换: 字符串转整数: 字节串转整数: 整数转字节串: 字符串转字节串: 字节串转字符串: 测试用的python源码 进行协议解析时,总是会遇到各种各样的数据 ...
- Python:字符串
一.序列的概念 序列是容器类型,顾名思义,可以想象,“成员”们站成了有序的队列,我们从0开始进行对每个成员进行标记,0,1,2,3,...,这样,便可以通过下标访问序列的一个或几个成员,就像C语言中的 ...
- python基础——字符串和编码
python基础——字符串和编码 字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用 ...
- python cookbook学习1
python cookbook学习笔记 第一章 文本(1) 1.1每次处理一个字符(即每次处理一个字符的方式处理字符串) print list('theString') #方法一,转列表 结果:['t ...
随机推荐
- iOS单例模式
单例模式用于当一个类只能有一个实例的时候, 通常情况下这个“单例”代表的是某一个物理设备比如打印机,或是某种不可以有多个实例同时存在的虚拟资源或是系统属性比如一个程序的某个引擎或是数据.用单例模式加以 ...
- Introduction to Face Detection and Face Recognition
http://www.shervinemami.info/faceRecognition.html http://docs.opencv.org/2.4/modules/contrib/doc/fac ...
- 阿里云数据库实例的一个db被开发人员删除了 如何恢复
1没有 逻辑备份的话. 如下操作即可 可以将那个临时实例的需要导的db用逻辑备份出来恢复到主实例就行了 好多朋友都在问,RDS中把数据恢复到7天内任意时间点的功能在哪里啊? 其实挺简单的,只需要五步操 ...
- struts配置测试中遇到报错信息,记录下
tomcat7 jdk7myeclipse2014 部署完成后,访问页面报错struts.xml文件内容: <?xml version="1.0" encoding=&quo ...
- 网页flv下载探索_1
最近看了一个优酷视频(非优酷网站,最终地址指向优酷),用chrome开发者工具,可找到flv地址如下,简单摘录如下: http://27.221.100.104/657D4D2878C3382C781 ...
- UIControl事件
1.UIControlEventTouchDown 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候. 2.UIControlEventTouchDownRepeat 多点触摸按下事件,点触计 ...
- RPC、SQL、NFS属于OSI的哪一层
第一层:物理层 第二层:数据链路层 802.2.802.3ATM.HDLC.FRAME RELAY 第三层:网络层 IP.IPX.ARP.APPLETALK.ICMP 第四层:传输层 TCP.UDP. ...
- NSTimer 线程操作
http://www.jianshu.com/p/0c050af6c5ee 2.NSTimer的创建与撤销必须在同一个线程操作.performSelector的创建与撤销必须在同一个线程操作.
- CentOS安装NodeJS v0.10.25 + Express
安装必需组件 yum -y install gcc make gcc-c++ openssl-devel wget cd ~wget http://nodejs.org/dist/v0.10.25/n ...
- PAT 解题报告 1051. Pop Sequence (25)
1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...