• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中的pprint折腾记

Python中的pprint折腾记

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python pprint,python pprint用法,python中pprint,pprint,pprint.pprint等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

1.背景

看到这里提到了pprint。 
打算去试试.

2.pprint简介

找到在线官网解释:

pprint — Data pretty printer 

就是一个,方便大家打印一些,相对复杂的变量的好东西。

3.使用pprint

去写点代码试试。


代码:
#-------------------------------------------------------------------------------
# Name:        【记录】折腾Python中的pprint
# Author:      Crifan Li
#
# Created:     06/01/2013
# Copyright:   (c) Crifan Li 2013
#-------------------------------------------------------------------------------

import pprint;
import re;

def pprintDemo():
    varsList = [
        [1, 2, 3],
        ["ab", "c", "def"],
        re.compile("\w+"),
        ("123", "abc"),
        {
            "key1":"value1",
            "key2":"value2",
        },
    ];

    for value in varsList:
        print value;

    print "-"*80;

    pp = pprint.PrettyPrinter(indent=4);
    for value in varsList:
        pp.pprint(value);

    print "="*80;
    stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'];
    stuff.insert(0, stuff[:]);
    print stuff;
    print "-"*80;
    pp.pprint(stuff)

if __name__ == '__main__':
    pprintDemo();
</div>

效果:

[1, 2, 3]
['ab', 'c', 'def']
<_sre.SRE_Pattern object at 0x00000000030DD378>
('123', 'abc')
{'key2': 'value2', 'key1': 'value1'}
--------------------------------------------------------------------------------
[1, 2, 3]
['ab', 'c', 'def']
<_sre.SRE_Pattern object at 0x00000000030DD378>
('123', 'abc')
{   'key1': 'value1', 'key2': 'value2'}
================================================================================
[['spam', 'eggs', 'lumberjack', 'knights', 'ni'], 'spam', 'eggs', 'lumberjack', 'knights', 'ni']
--------------------------------------------------------------------------------
[   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
    'spam',
    'eggs',
    'lumberjack',
    'knights',
    'ni']

</div>

4.总结

pprint,有点意思。

以后可以用在代码调试过程中。

</div>

您可能想查找下面的文章:

  • Python中使用pprint函数进行格式化输出的教程
  • Python中的pprint折腾记

相关文章

  • 研究Python的ORM框架中的SQLAlchemy库的映射关系
  • python计算最大优先级队列实例
  • Python读取图片EXIF信息类库介绍和使用实例
  • python统计日志ip访问数的方法
  • Python简单日志处理类分享
  • Python脚本暴力破解栅栏密码
  • Python单链表的简单实现方法
  • Python实现批量把SVG格式转成png、pdf格式的代码分享
  • 用Python脚本生成Android SALT扰码的方法
  • 一步步解析Python斗牛游戏的概率

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python去掉字符串中空格的方法
    • 使用Python内置的模块与函数进行不同进制的数的转换
    • 树莓派中python获取GY-85九轴模块信息示例
    • python使用正则搜索字符串或文件中的浮点数代码实例
    • 详解Python的Flask框架中生成SECRET_KEY密钥的方法
    • python文件和目录操作方法大全(含实例)
    • Python实现抓取城市的PM2.5浓度和排名
    • 用Python的Django框架来制作一个RSS阅读器
    • 结合Python的SimpleHTTPServer源码来解析socket通信
    • Python设计模式之观察者模式实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有