python3: 字符串和文本(4)
16. 以指定列宽格式化字符串[textwrap]
https://docs.python.org/3.6/library/textwrap.html#textwrap.TextWrapper
假如你有下列的长字符串:
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, 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, not
around the eyes, don't look around
the eyes, look into my eyes, you're
under.
17. 在字符串中处理html和xml(html.parse 或 xml.etree.ElementTree 自动处理了相关的替换细节)
直接使用html
>>> s = 'Elements are written as "<tag>text</tag>".'
>>> import html
>>> print(s)
Elements are written as "<tag>text</tag>".
>>> print(html.escape(s))
Elements are written as "<tag>text</tag>". >>> # Disable escaping of quotes
>>> print(html.escape(s, quote=False))
Elements are written as "<tag>text</tag>".
>>>
如果你正在处理HTML或者XML文本,试着先使用一个合适的HTML或者XML解析器
>>> s = 'Spicy "Jalapeño".'
>>> from html.parser import HTMLParser
>>> p = HTMLParser()
>>> p.unescape(s)
'Spicy "Jalapeño".'
>>>
>>> t = 'The prompt is >>>'
>>> from xml.sax.saxutils import unescape
>>> unescape(t)
'The prompt is >>>'
>>>
18. 字符串令牌解析(未)
python3: 字符串和文本(4)的更多相关文章
- [转]python3字符串与文本处理
转自:python3字符串与文本处理 阅读目录 1.针对任意多的分隔符拆分字符串 2.在字符串的开头或结尾处做文本匹配 3.利用shell通配符做字符串匹配 4.文本模式的匹配和查找 5.查找和替换文 ...
- python3字符串与文本处理
每个程序都回涉及到文本处理,如拆分字符串.搜索.替换.词法分析等.许多任务都可以通过内建的字符串方法来轻松解决,但更复杂的操作就需要正则表达式来解决. 1.针对任意多的分隔符拆分字符串 In [1]: ...
- python3: 字符串和文本(3)
11. 删除字符串中不需要的字符 strip() 方法能用于删除开始或结尾的字符: lstrip() 和 rstrip() 分别从左和从右执行删除操作 >>> s = ' hell ...
- python3: 字符串和文本(2)
6. 字符串忽略大小写的搜索替换 >>> text = 'UPPER PYTHON, lower python, Mixed Python' >>> re.find ...
- python3: 字符串和文本
1. 分割字符串-使用多个界定符[re.split()] >>> line = 'asdf fjdk; afed, fjek,asdf, foo' >>> impo ...
- Python3-Cookbook总结 - 第二章:字符串和文本
第二章:字符串和文本 几乎所有有用的程序都会涉及到某些文本处理,不管是解析数据还是产生输出. 这一章将重点关注文本的操作处理,比如提取字符串,搜索,替换以及解析等. 大部分的问题都能简单的调用字符串的 ...
- Python3 批量替换文本内容
Python3 批量替换文本内容 示例: # coding:utf8 import os; def reset(): i = 0 path = r"H:\asDemo\workdemo\aw ...
- 《Python CookBook2》 第一章 文本 - 过滤字符串中不属于指定集合的字符 && 检查一个字符串是文本还是二进制
过滤字符串中不属于指定集合的字符 任务: 给定一个需要保留的字符串的集合,构建一个过滤函数,并可将其应用于任何字符串s,函数返回一个s的拷贝,该拷贝只包含指定字符集合中的元素. 解决方案: impor ...
- python3字符串
Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...
随机推荐
- spring 下载地址及相关概念解析
http://www.springsource.org/download/community Release Candidate(RC)候选版本,用于指软件或操作系统的发布,一般与Debug版本相对应 ...
- [转]C#综合揭秘——Entity Framework 并发处理详解
本文转自:http://www.cnblogs.com/leslies2/archive/2012/07/30/2608784.html 引言 在软件开发过程中,并发控制是确保及时纠正由并发操作导致的 ...
- win10 uwp 商业游戏
本文告诉大家去做一个商业游戏,游戏很简单,几乎没有什么技术 游戏的开始,需要添加框架库,于是引用我自己写的库. 首先是创建一个启动页面,这个页面是显示启动的. 在显示启动的时候,是需要加载游戏需要使用 ...
- 泛型通配符extends与super的区别
<? extends T>限定参数类型的上界:参数类型必须是T或T的子类型 <? super T> 限定参数类型的下界:参数类型必须是T或T的超类型 总结为: <? ex ...
- 从mysql中dump数据到本地
方法一:使用mysqldump命令,如: mysqldump -h10.90.6.237 -uf_insplat2car_r -P3306 -pxxxxxxxxx nbmp tb_tag_log -- ...
- Mybatis源码正确打开方式
精心挑选要阅读的源码项目: 饮水思源——官方文档,先看文档再看源码: 下载源码,安装到本地,保证能编译运行: 从宏观到微观,从整体到细节: 找到入口,抓主放次,梳理核心流程: 源码调试,找到核心数据结 ...
- 湘潭校赛 Hard Wuxing
Hard Wuxing Accepted : 13 Submit : 166 Time Limit : 1000 MS Memory Limit : 65536 KB 题目描述 “五行”是中国 ...
- ef oracle参数化问题
并非所有变量都已绑定 假如一个sql是这样的 string sql =@" select id from a where date between :StartDate and :EndDa ...
- PHP生成缩略图(2)--等比缩略图
分析: 当原图是横屏或竖屏的时候,希望缩略图会保持原来的比例缩放,不改变原图的完整性,即等比缩放! 此时只需确定原图的宽高,以及目标图的最大宽高,比较目标图的宽高比例与原图的宽高比例的大小,以此来判断 ...
- 【代码笔记】iOS-iOS的目录
一.iOS中的沙盒机制 · iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.plist文件.sqlite数 ...