zoukankan      html  css  js  c++  java
  • python_表格形式输出

    1. 安装prettytable模块 pip install prettytable

    在Pycharm中的解释器中调用该模块的话,需要在该IDE环境下的terminal里安装,在外部运行py文件的话需要另外再安装一次,因为两者的路径不一致

    from prettytable import Prettytable

    2. 代码实例 

    def FinanceCalculator():
        '输入初始金额和月开销数,返回剩下的金额、当月的支出数、和最后的支出数'
        print 'Enter opening balance:',
        RawBalance = float(raw_input())
        Balance = RawBalance
        print 'Enter monthly payment:',
        MonthlyPayment = float(raw_input())
    
        print '		Amount	Remaining'
        table = PrettyTable(['Pymt#','Paid','Balance'])
        table.add_row(['---','---','---------'])
        i = 0
        while Balance>0:
            if i==0:
                paid = 0.00
            elif i>0 and Balance>=MonthlyPayment:
                paid = MonthlyPayment
            else:
                paid = Balance
            Balance = Balance-paid
            table.add_row([i,'$%.2f' % paid,'$%.2f' % Balance])
            i+=1
        table.sort_key(i)
        table.reversesort=False
        table.border = 0
        print table

    3. 输出效果

  • 相关阅读:
    __str__
    __call__
    私有成员
    @property
    静态方法
    静态字段
    cut qcut
    hive 函数大全
    sklearn 中的Countvectorizer/TfidfVectorizer保留长度小于2的字符方法
    numpy教程:随机数模块numpy.random
  • 原文地址:https://www.cnblogs.com/AHappyBird/p/9340784.html
Copyright © 2011-2022 走看看