以指定列宽格式化字符串

问题

你有一些长字符串,想以指定的列宽将它们重新格式化。

解法

使用 textwrap 模块来格式化字符串的输出。比如,假如你有下列的长字符串:

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."

下面演示使用 textwrap 格式化字符串的多种方式:

>>> import textwrap
>>> print(textwrap.fill(s, 70))
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.
>>> print(textwrap.fill(s, 40))
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.>>> print(textwrap.fill(s, 40, initial_indent='     '))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.>>> print(textwrap.fill(s, 40, subsequent_indent='     '))
Look into my eyes, look into my eyes,the eyes, the eyes, the eyes, notaround the eyes, don't look aroundthe eyes, look into my eyes, you'reunder.

讨论

textwrap 模块对于字符串打印是非常有用的,特别是当你希望输出自动匹配终端大小的时候。你可以使用 os.get terminal size() 方法来获取终端的大小尺寸。比如:

>>> import os
>>> os.get_terminal_size().columns
80
>>>

fill() 方法接受一些其他可选参数来控制tab,语句结尾等。 参阅 textwrap.TextWrapper 文档 获取更多内容。

《Python Cookbook 3rd》笔记(2.16):以指定列宽格式化字符串相关推荐

  1. 《Python Cookbook 3rd》笔记汇总

    文章目录 一.数据结构 二.字符串和文本 三.数字.日期和时间 四.迭代器与生成器 五.文件与IO 一.数据结构 标题 关键词 1.1:拆分序列后赋值给多个变量 可迭代对象.拆分赋值 1.2:拆分任意 ...

  2. 《Python cookbook》笔记二

    <Python cookbook>笔记二 第二章 字符串和文本 -使用多个界定符分割字符串- 你需要将一个字符串分割为多个字段,但是分隔符 (还有周围的空格) 并不是固定 的. # str ...

  3. 《Python Cookbook 3rd》笔记(1.4):查找最大或最小的N个元素

    <Python Cookbook 3rd>1.4:查找最大或最小的N个元素 问题 怎样从一个集合中获得最大或者最小的N个元素列表? 解法 heapq 模块有两个函数:nlargest()和 ...

  4. Machine Learning with Python Cookbook 学习笔记 第9章

    Chapter 9. Dimensionality Reduction Using Feature Extraction 前言 本笔记是针对人工智能典型算法的课程中Machine Learning w ...

  5. Machine Learning with Python Cookbook 学习笔记 第6章

    Chapter 6. Handling Text 本笔记是针对人工智能典型算法的课程中Machine Learning with Python Cookbook的学习笔记 学习的实战代码都放在代码压缩 ...

  6. Python Cookbook 3rd Edition Documentation

    Python Cookbook 3rd Edition Documentation 文章目录 第一章:数据结构和算法 1.1 解压序列赋值给多个变量 问题 解决方案 讨论 1.2 解压可迭代对象赋值给 ...

  7. Machine Learning with Python Cookbook 学习笔记 第8章

    Chapter 8. Handling Images 前言 本笔记是针对人工智能典型算法的课程中Machine Learning with Python Cookbook的学习笔记 学习的实战代码都放 ...

  8. java设置列宽_javaexcel如何设置指定列宽

    Java 开发过程当中经常会遇到java表格操作的场景.如何实现轻松.快捷的设置列宽呢,下面让学习啦小编为你带来javaexcel设置指定列宽的方法. javaexcel设置指定列宽的方法: 方法1 ...

  9. 《Python Cookbook 3rd》笔记(2.13):字符串对齐

    字符串对齐 问题 你想通过某种对齐方式来格式化字符串 解法 对于基本的字符串对齐操作,可以使用字符串的 ljust() , rjust() 和 center()方法.比如: >>> ...

最新文章

  1. 遍历Treeview每个节点并初始化(C#)
  2. 【Flutter】如何写一个Flutter自动打包成iOS代码模块的脚本
  3. Libgdx学习笔记:UI之评分组件
  4. [你必须知道的.NET]第九回:品味类型---值类型与引用类型(中)-规则无边
  5. 开发板、Windows、Ubuntu三者互联——韦东山嵌入式Linux学习笔记08
  6. 【thymeleaf】【SpringBoot】Thymeleaf 获取.properties中的配置项变量
  7. linux抓post命令,Linux 使用curl发起post请求的4个常用方式
  8. nuget 包管理器
  9. linux 4.4内核特性,Linux Kernel 4.4.19 LTS长期支持版发布
  10. IDEA中启动tomcat
  11. forever不重启 node_node.js – NodeJS和Forever(监控并重启应用)
  12. k8s之常用操作命令
  13. 如何提取mp4中的音频?
  14. java多态的练习 ,定义三个类,父类GeometricObject代表几何形状,子类Circle代表圆形,MyRectangle代表矩形。 定义一个测试类GeometricTest,编写equals
  15. php接入飞鹅打印机
  16. thonny python介绍_面向初学者的 Python IDE:Thonny
  17. 2007word如何转PDF
  18. C# 用 iso-8859-5 (cyillic)字符转换的方法, 确定Unicode文字中是否有俄语字母
  19. IOS开发工具介绍之Xcode开发工具使用
  20. 真实生活的记录:我三年的外企生涯(2) 出处:天涯虚拟社区

热门文章

  1. 自己动手写TCC7901的GPIO驱动
  2. c语言e怎么表示_来测测!这11个C语言入门基础知识你都掌握了吗?
  3. docker php composer 使用_「PHP编程」如何使用Docker制作自己的LNMP/LAMP镜像
  4. wordpress 后台慢_建站经验-wordpress用户注册收不到验证邮件
  5. go MySQL 多语句_八、MySQL经典查询语句-Go语言中文社区
  6. html多出的空白页怎么删除,word多出一页空白页怎么删除,这4个方法总有一个能解决,真实挂机网赚项目...
  7. Arcmap格式转arcgis的shp格式
  8. 【转】如何判断一个文本文件内容的编码格式 UTF-8 ? ANSI(GBK)
  9. [你必须知道的.NET]第十六回:深入浅出关键字---using全接触
  10. 【转】ELK是什么能做什么怎么做