【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 ...
随机推荐
- Oracle直方图导致SQL不走索引.
在ITPUB 上看到一个帖子 http://www.itpub.net/thread-1875212-1-1.html 同一条SQL语句,只有查询条件不一样,查询返回的结果集都为0,一个走了全表扫描, ...
- Android:学习AIDL,这一篇文章就够了(下)
前言 上一篇博文介绍了关于AIDL是什么,为什么我们需要AIDL,AIDL的语法以及如何使用AIDL等方面的知识,这一篇博文将顺着上一篇的思路往下走,接着介绍关于AIDL的一些更加深入的知识.强烈建议 ...
- def文件格式
模块定义 (.def) 文件 模块定义 (.def) 文件为链接器提供有关被链接程序的导出.属性及其他方面的 信息.生成 DLL 时,.def 文件最有用.由于存在 ...
- SQLdiag-配置文件-ProfilerCollector
上一篇,我们讲述了配置文件中与性能计数器相关的PerfmonCollector元素:这一篇我们将讲述与跟踪数据相关的ProfilerCollector元素.在上一篇中使用SD_Detailed.XML ...
- [PCL]1 PCL点云库安装
1.安装文件下载:官网,我还是比较喜欢别人编译好的安装包啊,哈哈. http://www.pointclouds.org/downloads/windows.html 2.傻瓜式安装(下面的依赖项都集 ...
- MongoDB 正则表达式
示例 MongoDB 使用 $regex 操作符来设置匹配字符串的正则表达式. > db.col.find() { "_id" : ObjectId("56c6bb ...
- Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)
控制台程序. java.awt.GridBagLayout管理器比前面介绍的其他布局管理器灵活得多,因此使用起来也比较复杂.基本机制就是在随意的矩形网格中布局组件,但网格的行和列不一定拥有相同的高度和 ...
- 在Ubuntu 64位OS上运行hadoop2.2.0[重新编译hadoop]
最近在学习搭建Hadoop, 我们从Apache官方网站直接下载最新版本Hadoop2.2.官方目前是提供了linux32位系统可执行文件,结果运行时发现提示 “libhadoop.so.1.0.0 ...
- 《30天自制操作系统》10_day_学习笔记
harib07a: 整理内存管理函数:memman_alloc和memman_free能够以最小1字节进行内存管理,但时间久了后,容易产生外部碎片:为此,笔者编写了一些以0x1000字节为单位进行内存 ...
- 比较Date时间先后
if ([firstDetailSelect compare:secondDetailSelect] == NSOrderedDescending) { [MBProgressHUD showErro ...