问题:重新格式化一些很长的字符串,以指定的列数来显示

解决方案: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.以固定的列数重新格式化文本的更多相关文章

  1. python cookbook 字符串和文本

    使用多个界定符分隔字符串 import re line = 'asdf fjdk; afed, fjek,asdf, foo' print(re.split(r'[;,\s]\s*', line)) ...

  2. python书籍推荐:Python Cookbook第三版中文

    所属网站分类: 资源下载 > python电子书 作者:熊猫烧香 链接:http://www.pythonheidong.com/blog/article/44/ 来源:python黑洞网 内容 ...

  3. python 如何将md5转为16字节

    python的hashlib库中提供的hexdigest返回长度32的字符串. md5sum是128bit,也就是16字节,如何将python生成字符串的转为16字节呢? 请看下面代码 import ...

  4. 【python cookbook】【字符串与文本】9.将Unicode文本统一表示为规范形式

    问题:确保所有的Unicode字符串都拥有相同的底层 解决方案:为解决同一个文本拥有多种不同的表示形式问题,应该先将文本统一表示为规范形式,这可以通过unicodedata模块来完成, unicode ...

  5. python常用的十进制、16进制、字符串、字节串之间的转换

    进行协议解析时,总是会遇到各种各样的数据转换的问题,从二进制到十进制,从字节串到整数等等 废话不多上,直接上例子 整数之间的进制转换: 10进制转16进制: hex(16)  ==>  0x10 ...

  6. [转]python常用的十进制、16进制、字符串、字节串之间的转换

    阅读目录(Content) 整数之间的进制转换: 字符串转整数: 字节串转整数: 整数转字节串: 字符串转字节串: 字节串转字符串: 测试用的python源码 进行协议解析时,总是会遇到各种各样的数据 ...

  7. Python:字符串

    一.序列的概念 序列是容器类型,顾名思义,可以想象,“成员”们站成了有序的队列,我们从0开始进行对每个成员进行标记,0,1,2,3,...,这样,便可以通过下标访问序列的一个或几个成员,就像C语言中的 ...

  8. python基础——字符串和编码

    python基础——字符串和编码 字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用 ...

  9. python cookbook学习1

    python cookbook学习笔记 第一章 文本(1) 1.1每次处理一个字符(即每次处理一个字符的方式处理字符串) print list('theString') #方法一,转列表 结果:['t ...

随机推荐

  1. 面试&笔试常见题,你了解多少?

    HTML:1.  什么是语义化的HTML?有何意义?为什么要做到语义化?(高频率考题)2.  行内元素和块元素分别有哪些?(高频率)3.  严格模式与混杂模式的区分?如何触发这两种模式?(高频率)4. ...

  2. convert Timestamp to Real time

    select r.ring_buffer_address, r.ring_buffer_type, dateadd (ms, r.[timestamp] - sysinfo.sqlserver_sta ...

  3. Docker容器管理

    容器是Docker第二个核心概念,简单的的说容器是镜像的一个运行实例,所不同的是,它带有额外的可写文件层. 如果说虚拟机是模拟运行了一整套操作系统(提供运行态环境和其他系统环境)和跑在上面的应用.那么 ...

  4. 正确统计SQLServer的慢日志

    RDS的一个富有吸引力的服务是为用户提供慢日志的运行状况报告.报告从不同的维度(总执行时间,总执行次数,总逻辑读,总物理读)为用户提供TOP20的SQL.RDS希望在为用户提供稳定,快速服务的同时,用 ...

  5. C#高级编程 反射 代码示例

    反射 反射(Reflection)是.NET中的重要机制,通过反射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等. 还可以获得每 ...

  6. [PCL]keypoint

    1. Harris角点 http://www.cnblogs.com/ironstark/p/5064848.html 2. ISS关键点 http://www.cnblogs.com/ironsta ...

  7. [算法]Comparison of the different algorithms for Polygon Boolean operations

    Comparison of the different algorithms for  Polygon Boolean operations. Michael Leonov 1998 http://w ...

  8. [SharpMap]二叉树索引

    读取shp文件建立二叉树索引的基本思路分析: /// <summary> /// Generates a spatial index for a specified shape file. ...

  9. NETBEANS + XDEBUG + IIS PHP 代码 调试 DEBUG

    参考: http://domainwebcenter.com/?p=936 http://www.sitepoint.com/debugging-and-profiling-php-with-xdeb ...

  10. javascript中数组的concat()方法 - 数组连接

    <html> <head> <title>数组的concat()方法</title> <script> /* 数组的concat()方法: ...