当前位置: 首页 > news >正文

国外做家谱的网站/汕头seo网站推广

国外做家谱的网站,汕头seo网站推广,做视频网站 买带宽,h5网站开发软件下载【python】pprint格式化输出 目录 【python】pprint格式化输出目录起因分析应用尾声起因 偶然看到这样一个 提问 。提问如下: a{1: 2, 3: 4} pprint.pprint(a){1: 2, 3: 4}为什么不是格式化输出呢? In [10]: pprint.pprint(sys.path) [,/usr/bin,/usr/lo…

【python】pprint格式化输出¶

目录¶

  • 【python】pprint格式化输出
  • 目录
  • 起因
  • 分析
  • 应用
  • 尾声

起因¶

偶然看到这样一个 提问 。提问如下:

a={1: 2, 3: 4}
pprint.pprint(a){1: 2, 3: 4}

为什么不是格式化输出呢?

In [10]: pprint.pprint(sys.path)
['','/usr/bin','/usr/local/lib/python2.7/dist-packages/pysam-0.6-py2.7-linux-x86_64.egg','/usr/local/lib/python2.7/dist-packages/Cython-0.17-py2.7-linux-x86_64.egg','/usr/local/lib/python2.7/dist-packages/pip-1.2.1-py2.7.egg','/usr/lib/pymodules/python2.7','/usr/local/lib/python2.7/dist-packages/ruffus-2.2-py2.7
}

不是应该一行行输出字典吗?求解。

这个格式化输出我以前没用过,自己也就跟着去学习了一下。

分析¶

在本机上重新输入了问题的代码,果然如提问所叙。当我尝试把字典a改的较为复杂时,则可以实现分行了。 那么初步判断,应该是行宽的问题。查看pprint.pprint()的源码,

def pprint(object, stream=None, indent=1, width=80, depth=None):"""Pretty-print a Python object to a stream [default is sys.stdout]."""printer = PrettyPrinter(stream=stream, indent=indent, width=width, depth=depth)printer.pprint(object)

可以看到里面的参数width,这个从字面来看,应该是控制行宽的。 进一步来看一下源文件的注释。

"""Handle pretty printing operations onto a stream using a set of
configured parameters.indent
    Number of spaces to indent for each level of nesting.width
    Attempted maximum number of columns in the output.depth
    The maximum depth to print out nested structures.stream
    The desired output stream.  If omitted (or false), the standard
    output stream available at construction will be used."""

尝试把默认值80修改为1。发现就可以实现分行了。

a={1: 2, 3: 4}
pprint.pprint(a, width=1){1: 2,
3: 4}

至此,可以解答提问中的疑问了

应用¶

那么对于自己来说,这个pprint有什么用呢?

我想,这至少是一个很好的json解析工具。 每次面对相当繁杂的json串时,每每都要去上网使用 在线json格式化工具 ,相当之繁杂。 现在,可以尝试使用pprint来进行格式化。

def main():import pprinta =  { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }],"authors": [{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }],"musicians": [{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }] }pprint.pprint(a,width=1)
if __name__=="__main__":main()

输出结果为:

{'authors': [{'firstName': 'Isaac','genre': 'science fiction','lastName': 'Asimov'},{'firstName': 'Tad','genre': 'fantasy','lastName': 'Williams'},{'firstName': 'Frank','genre': 'christian fiction','lastName': 'Peretti'}],'musicians': [{'firstName': 'Eric','instrument': 'guitar','lastName': 'Clapton'},{'firstName': 'Sergei','instrument': 'piano','lastName': 'Rachmaninoff'}],'programmers': [{'email': 'aaaa','firstName': 'Brett','lastName': 'McLaughlin'},{'email': 'bbbb','firstName': 'Jason','lastName': 'Hunter'},{'email': 'cccc','firstName': 'Elliotte','lastName': 'Harold'}]}

perfect! 对于字符串可以做同样的处理。只是需要将字符串首先转换为Python对象。然后进行处理。

def main():import pprinta = "{'programmers': [{'lastName': 'McLaughlin', 'email': 'aaaa', 'firstName': 'Brett'}, {'lastName': 'Hunter', 'email': 'bbbb', 'firstName': 'Jason'}, {'lastName': 'Harold', 'email': 'cccc', 'firstName': 'Elliotte'}], 'musicians': [{'lastName': 'Clapton', 'firstName': 'Eric', 'instrument': 'guitar'}, {'lastName': 'Rachmaninoff', 'firstName': 'Sergei', 'instrument': 'piano'}], 'authors': [{'genre': 'science fiction', 'lastName': 'Asimov', 'firstName': 'Isaac'}, {'genre': 'fantasy', 'lastName': 'Williams', 'firstName': 'Tad'}, {'genre': 'christian fiction', 'lastName': 'Peretti', 'firstName': 'Frank'}]}"
b = eval(a)
pprint.pprint(b, width=1)
if __name__=="__main__":main()

当然,你也可以修改输出流(stream参数),缩进(indent参数)等丰富你的功能。

尾声¶

再来看pprint的源码,突然看到开头有这样一段注释。

#  Author:      Fred L. Drake, Jr.
#               fdrake@acm.org
#
#  This is a simple little module I wrote to make life easier.  I didn't
#  see anything quite like it in the library, though I may have overlooked
#  something.  I wrote this when I was trying to read some heavily nested
#  tuples with fairly non-descriptive content.  This is modeled very much
#  after Lisp/Scheme - style pretty-printing of lists.  If you find it
#  useful, thank small children who sleep at night.

感谢那些夜晚乖乖睡觉的孩子们。

转载于:https://www.cnblogs.com/ToBeSmart/p/3641019.html

http://www.lbrq.cn/news/832141.html

相关文章:

  • 做打鱼网站/中国50强企业管理培训机构
  • 邵阳疫情最新消息今天又封了/抖音seo是什么
  • 网站开发背景图模板/郑州外语网站建站优化
  • 上虞网站建设baidu/百度客服人工电话
  • 开发网站用得最多的是什么语言/住房和城乡建设部
  • 自己做产品网站/网站优化的意义
  • 网站的登录功能一般是用cookie做的/百度一下官方下载安装
  • 企业手机网站开发/做网络推广的网站有哪些
  • 广西建设工程管理网站/每日精选12条新闻
  • 网站风格 颜色搭配/sem和seo的区别
  • 网站新闻源码/网页设计与制作项目教程
  • 富阳做兼职的网站/阿里云搜索引擎
  • 网站项目设计与制作综合实训/北京seo优化推广
  • 化妆品的网站设计方案/seo权重查询
  • 常州模板建站哪家好/关键词排名监控
  • 内网电脑做网站/四川seo技术培训
  • dw做了网站还可以做淘宝详情吗/广告投放公司
  • seo关键词优化服务/天津seo优化公司
  • 中国icp备案网站/app拉新推广平台渠道商
  • 注册个网站域名多少钱一年/应用宝aso优化
  • 衡东网站制作/志鸿优化网
  • 高中生做网站网页/网站推广方案策划书2000
  • 怎么用frontpage做网站/海淀区seo搜索引擎
  • 南通制作手机网站/图片百度搜索
  • html5 js全屏滑动网站源码/郴州seo网络优化
  • 竞价单页网站模板/seo的收费标准
  • 网站开发总结标题/广告推广免费
  • 唐山网站制作企业/seo数据
  • 网站开发实战 课程/seo优化服务商
  • 哪些网站开发/今天今日头条新闻
  • Java大视界:Java大数据在智能医疗电子健康档案数据挖掘与健康服务创新>
  • 机器学习基础:线性回归算法详解(原理+代码+实战)
  • Git 团队协作完全指南:从基础到高级应用
  • 前端基础——B/S工作原理、服务器与前端三大件
  • 防爆手机是什么?能用普通手机改装吗?
  • Linux系统安装Docker及部署Node.js 20.15.0(含pnpm、pm2)完整指南