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 [] 通过索引获取字符串中 ...
随机推荐
- golang基础--细说defer
defer 匿名函数特性 执行方式类似其它语言中的析构函数,在函数体执行结束后按照调用顺序的相反顺序逐个执行 //执行顺序相反 package main import "fmt" ...
- BATJ面试必会之Java IO 篇
一.概览 二.磁盘操作 三.字节操作 实现文件复制 装饰者模式 四.字符操作 编码与解码 String 的编码方式 Reader 与 Writer 实现逐行输出文本文件的内容 五.对象操作 序列化 S ...
- C#语法之委托和事件
从大学就开始做C#这块,也做C#几年了,最近又从ios转回.Net,继续做C#,之前也没有写博客的习惯,写博客也是从我做ios的时候开始的,现在既然又做回了.net,那就写点关于.Net的博客,可能在 ...
- Head First Python学习笔记2——文件与异常
文件处理 1.用open()就可以打开文件,但是请注意:文件里有中文请设置编码,如 :open("filepath","r",encoding="ut ...
- 【转】Stack Overflow研发副总裁:.NET技术并不差,合适自己就好
摘要:在QCon纽约大会上, Stack Exchange的工程部副总裁David Fullerton深入解析了如何使用C#.MS SQL等技术支撑Stack Overflow网站的单块应用架构,这个 ...
- C盘空间被占满的原因
如何禁止IIS在C:\Windows\System32\LogFiles\HTTPERR中生成日志文件C:\Program Files\Microsoft SQL Server\MSSQL10_50. ...
- [日常] mysql的索引使用情况测试
1.索引(Index)是帮助MySQL高效获取数据的数据结构,可以理解为“排好序的快速查找数据结构”,在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数据, ...
- C++读取配置文件
在牛人的指导下,和前一个版本有了较大改变. 逐行读取配置文件,然后逐行解析~ 读取一次之后,将键值对存入map,之后都从map中去取,减少读取文件次数 主要代码如下: /** * * read con ...
- ef oracle参数化问题
并非所有变量都已绑定 假如一个sql是这样的 string sql =@" select id from a where date between :StartDate and :EndDa ...
- cakephp中使用大括号的形式避免用点号连接sql语句
在cakephp中可以使用{}的形式来代替点号连接sql语句,减少出错的几率