美图齐众专注资阳网站设计 资阳网站制作 资阳网站建设
资阳网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

创新互联Python教程:pythonpprint.pformat()函数的使用

1、返回要打印的内容的文本字符串,这个字符串既易于阅读,也是语法上正确的python代码。

import  pprint
cats = [{'name':'Zophie','desc':'chubby'},{'name':'Pooka','desc':'fluffy'}]
pprint.pformat(cats)
 
fileObj = open('myCats.py','w')
fileObj.write('cats = '+pprint.pformat(cats)+'\n')
fileObj.close()

2、如果来自pprint.pformat()的字符串保存为.py文件,该文件就是可以导入的模块。

import myCats
myCats.cats
 
myCats.cats[0]
 
myCats.cats[0]['name']

以上就是python pprint.pformat()函数的使用,希望对大家有所帮助。更多Python学习指路:创新互联python教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。


分享文章:创新互联Python教程:pythonpprint.pformat()函数的使用
标题URL:http://www.zsjierui.cn/article/ccecgpj.html

其他资讯