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. 字符串令牌解析(未)

  • 相关阅读:
    Html 表单表格 form table
    JavaWeb -- 服务器传递给Servlet的对象 -- ServletConfig, ServletContext,Request, Response
    JavaWeb -- Servlet运行过程 和 细节
    调用DLL中的过程和函数
    调用DLL中的过程和函数
    动态载入 DLL
    动态载入 DLL
    静态载入 DLL
    DLL的加载和调用
    静态载入 DLL
  • 原文地址:https://www.cnblogs.com/xiyuan2016/p/10341854.html
Copyright © 2011-2022 走看看