• textwrap:格式化文本段落


    介绍

    需要美观打印(pretty-printing)的情况下,可以使用textwrap模块格式化要输出的文本。
    它提供了很多文本编辑器和字符处理器中都有的段落自动换行或填充特性

    填充段落

    import textwrap
    
    text = '''
        There are moments in life when you miss someone
        so much that you just want to pick them
        from your dreams and hug them for real! Dream what
        you want to dream;go where you want to go;
        be what you want to be,because you have
        only one life and one chance to do all the things you want to do.
    '''
    # fill函数取文本作为输入,生成格式化文本作为输出
    # width表示宽度为50
    print(textwrap.fill(text, width=50))
    '''
         There are moments in life when you miss
    someone      so much that you just want to pick
    them      from your dreams and hug them for real!
    Dream what      you want to dream;go where you
    want to go;     be what you want to be,because you
    have      only one life and one chance to do all
    the things you want to do.
    '''
    

    结果不是太让人满意。文本虽然已经对齐,不过只有第一行保留了缩进,后面各行的空格则嵌入在段落中

    去除现有的缩进

    import textwrap
    
    text = '''
        There are moments in life when you miss someone
        so much that you just want to pick them
        from your dreams and hug them for real! Dream what
        you want to dream;go where you want to go;
        be what you want to be,because you have
        only one life and one chance to do all the things you want to do.
    '''
    
    # 关于刚才的例子,其输出中混合嵌入了制表符和额外的空格,所以格式不是太美观。
    # 用dedent可以去除示例文本中所有的行前面的空白符,这会生成更好的结果
    # 并且允许在Python代码中直接使用docstring或者内嵌的多行字符串,同时去除代码本身的格式。
    print(textwrap.dedent(text).strip())
    '''
    There are moments in life when you miss someone
    so much that you just want to pick them
    from your dreams and hug them for real! Dream what
    you want to dream;go where you want to go;
    be what you want to be,because you have
    only one life and one chance to do all the things you want to do.
    '''
    

    可以看到dedent作用就是把每一行开头的缩进给去掉

    结合dedent和fill

    import textwrap
     
     
    text = '''
        There are moments in life when you miss someone
        so much that you just want to pick them
        from your dreams and hug them for real! Dream what
        you want to dream;go where you want to go;
        be what you want to be,because you have
        only one life and one chance to do all the things you want to do.
    '''
    
    # 先去除缩进,然后填充,每行长度60个字符
    dedent_text = textwrap.dedent(text).strip()
    print(textwrap.fill(dedent_text, width=60))
    '''
    There are moments in life when you miss someone  so much
    that you just want to pick them  from your dreams and hug
    them for real! Dream what  you want to dream;go where you
    want to go; be what you want to be,because you have  only
    one life and one chance to do all the things you want to do.
    '''
    

    缩进块

    import textwrap
     
     
    text = '''
        There are moments in life when you miss someone
        so much that you just want to pick them
        from your dreams and hug them for real! Dream what
        you want to dream;go where you want to go;
        be what you want to be,because you have
        only one life and one chance to do all the things you want to do.
    '''
    # 可以使用indent函数为一个字符串的所有行增加一致的前缀文本
    dedent_text = textwrap.dedent(text).strip()
    final_text = textwrap.indent(dedent_text, ">>>")
    print(final_text)
    '''
    >>>There are moments in life when you miss someone
    >>>so much that you just want to pick them
    >>>from your dreams and hug them for real! Dream what
    >>>you want to dream;go where you want to go;
    >>>be what you want to be,because you have
    >>>only one life and one chance to do all the things you want to do.
    '''
     
    # 除此之外还可以给指定行添加
    final_text = textwrap.indent(dedent_text, prefix="->", predicate=lambda line: len(line.strip()) > 40)
    print(final_text)
    '''
    ->There are moments in life when you miss someone
    so much that you just want to pick them
    ->from your dreams and hug them for real! Dream what
    ->you want to dream;go where you want to go;
    be what you want to be,because you have
    ->only one life and one chance to do all the things you want to do.
    '''
    # 显然lambda里面的line就是每一行的文本,指定文本长度大于40的
    

    悬挂缩进

    import textwrap
     
     
    text = '''
        There are moments in life when you miss someone
        so much that you just want to pick them
        from your dreams and hug them for real! Dream what
        you want to dream;go where you want to go;
        be what you want to be,because you have
        only one life and one chance to do all the things you want to do.
    '''
    # 不仅可以设置输出的宽度,还可以采用同样的方式单独控制首行的缩进,使首行的缩进不同于后续的各行
    dedent_text = textwrap.dedent(text).strip()
    print(textwrap.fill(dedent_text,
                        initial_indent="",
                        subsequent_indent=" "*4,
                        width=50))
    '''
    There are moments in life when you miss someone
        so much that you just want to pick them  from
        your dreams and hug them for real! Dream what
        you want to dream;go where you want to go; be
        what you want to be,because you have  only one
        life and one chance to do all the things you
        want to do.
    '''
    # 这便可以生成悬挂缩进,即首行缩进小于其他行的缩进
    # 缩进值也可以包含非空白字符。
    print(textwrap.fill(dedent_text,
                        initial_indent="",  # 首行缩进
                        subsequent_indent="*"*4,  # 首行之外的行缩进
                        width=50))
    '''
    There are moments in life when you miss someone
    ****so much that you just want to pick them  from
    ****your dreams and hug them for real! Dream what
    ****you want to dream;go where you want to go; be
    ****what you want to be,because you have  only one
    ****life and one chance to do all the things you
    ****want to do.
    '''
    

    截断长文本

    import textwrap
     
     
    text = '''
        There are moments in life when you miss someone
        so much that you just want to pick them
        from your dreams and hug them for real! Dream what
        you want to dream;go where you want to go;
        be what you want to be,because you have
        only one life and one chance to do all the things you want to do.
    '''
    dedent_text = textwrap.dedent(text).strip()
    print(textwrap.shorten(dedent_text, width=50))  # There are moments in life when you miss [...]
    
  • 相关阅读:
    视频直播技术-视频-编码-传输-秒开等<转>
    弹出框JBox实例
    Dijkstra in python
    oracle 修改索引现有表空间
    WIN7 如何关闭Aero
    不再打酱油
    Android 开机默认横竖屏
    cocos2d-x 3.0 开发(一) Hello_New_World
    PHOTOSHOP 中画笔工具和铅笔工具的一个小小差别
    一种从JSON数据创建Java类的高效办法
  • 原文地址:https://www.cnblogs.com/traditional/p/11853704.html
Copyright © 2020-2023  润新知