zoukankan      html  css  js  c++  java
  • 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 &quot;&lt;tag&gt;text&lt;/tag&gt;&quot;.
    
    >>> # Disable escaping of quotes
    >>> print(html.escape(s, quote=False))
    Elements are written as "&lt;tag&gt;text&lt;/tag&gt;".
    >>>

     如果你正在处理HTML或者XML文本,试着先使用一个合适的HTML或者XML解析器

    >>> s = 'Spicy &quot;Jalape&#241;o&quot.'
    >>> from html.parser import HTMLParser
    >>> p = HTMLParser()
    >>> p.unescape(s)
    'Spicy "Jalapeño".'
    >>>
    >>> t = 'The prompt is &gt;&gt;&gt;'
    >>> from xml.sax.saxutils import unescape
    >>> unescape(t)
    'The prompt is >>>'
    >>>

    18. 字符串令牌解析(未)

  • 相关阅读:
    Django安装和启动
    转载:Python 包管理工具解惑
    电子商务的基本理念
    javascript的循环使用
    错误码
    weex初始
    Flex 布局
    css样式重置表
    手机端页面自适应解决方案—rem布局
    实用的60个CSS代码片段[下]
  • 原文地址:https://www.cnblogs.com/xiyuan2016/p/10341854.html
Copyright © 2011-2022 走看看