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. 字符串令牌解析(未)
标签:eyes,look,into,textwrap,字符串,文本,my,python3,around 来源: https://www.cnblogs.com/xiyuan2016/p/10341854.html