• python-write_text


    1、python用prettytable输出漂亮的表格

    https://linuxops.org/blog/python/prettytable.html

    2、写文件

    f = open('C:\Users\hfqn\Desktop\test.txt', 'w')
    f.write('Hello, world!')
    f.close()

    3、打印表格文本,自动对齐:

    demo:

    def format_table(columns_name, data):
        max_len = 0
        for col in columns_name:
            if max_len < len(col):
                max_len = len(col)
        for d in data[0]:
            if max_len < len(str(d)):
                max_len = len(str(d))
        line_str = "+--------{}--------".format("-"*(2*max_len))
        first_line = line_str * len(columns_name) + "+"
        column_line = ''
        lens = len(line_str)
        for col in columns_name:
            blank = ' ' * (int((lens - len(col) * 2) / 2))
            column_line = "{}|{}{}{}".format(column_line, blank, col, blank)
    
        data_lines = None
        for i, data_line in enumerate(data):
            n1 = (int((lens - len(str(i))) / 2))
            n2 = n1
            if n1*2+len(str(i))+1 > lens:
                n2 = n1 - 1
            if n1 * 2 + len(str(i))+1 < lens:
                n2 = n1 + 1
            line = "|{}{}{}".format(' '*n1, i, ' '*n2)
            n1 = (int((lens - len(str(data_line))) / 2))
            n2 = n1
            if n1 * 2 + len(str(data_line))+1 > lens:
                n2 = n1 - 1
            if n1 * 2 + len(str(data_line))+1 < lens:
                n2 = n1 + 1
            line = "{}|{}{}{}".format(line, ' '*n1, data_line, ' '*n2)
            if data_lines:
                data_lines = "{}
    {}{}".format(data_lines, line, '|')
            else:
                data_lines = "{}{}".format(line, '|')
        result = "{}
    {}|
    {}
    {}
    {}".format(first_line, column_line, first_line, data_lines, first_line)
        return result

    3、

  • 相关阅读:
    移动端 app
    python 3.8 新特性
    vue 路由歪招
    VUE 关于组件依赖的问题
    vue 全局注册组件
    CSS小技巧
    vue踩坑记 页面跳转不新
    vuecli eslint 语法错误解决办法
    vue v-slot用法测试
    终止 IdFtp下载
  • 原文地址:https://www.cnblogs.com/rnanprince/p/10851341.html
Copyright © 2020-2023  润新知