python PRETTY PRINTING

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python PRETTY PRINTING相关的知识,希望对你有一定的参考价值。

def py_format(input, file_ot):
  '''
  Name: py_format
  Description: Outputs a file that contains the 
               definition of <input> ready to be
               imported (See Notes and sample code)
  Input: <input data structure> <filename>
  Output: <filename> that contains input in python format
  Usage: py_format(project_info, "OUTDATA.py")
  Notes: 
        py_format(project_info, "OUTDATA.py")
        import OUTDATA
        data = OUTDATA.allData
  Notes2: Check also in 'Python Useful Tricks'
  '''
    # check if file_ot ends in .py
    if file_ot.lower().endswith('.py'):
        try:
            resultFile = open(file_ot, 'w')
            resultFile.write('allData = ' + pprint.pformat(input))
            resultFile.close()
        except:
            print("Problem loading data to: " + str(file_ot))
            return False
        else:
            return True
def pretty_print(input, indent_depth):
  '''
  Name: pretty_print
  Description: pretty_prints a data structure
  Input: <List/Tuple/Dict>, indent_depth
  Output: stdout
  Usage: pretty_print(dict_in, 4)
  Notes: Works on any kind of data structure. 
  Requires: pprint module
  '''
    import pprint
    try:
        pprint.pprint(input)
        #pprint.PrettyPrinter(indent=indent_depth)
    except:
        print("Pretty print failed")
        

以上是关于python PRETTY PRINTING的主要内容,如果未能解决你的问题,请参考以下文章

gdb调试遇到的问题

python 来自http://stackoverflow.com/questions/328851/printing-all-instances-of-a-class

pretty-errors:美化python异常输出以使其清晰易读

使用python输出Sympy乳胶

如何在 Python 中使用 Pretty Table 打印多个列表中的数据?

飘逸的python - 实现一个pretty函数美丽的输出嵌套字典