新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:pythonpprint.pformat()函數(shù)的使用
1、返回要打印的內(nèi)容的文本字符串,這個字符串既易于閱讀,也是語法上正確的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文件,該文件就是可以導(dǎo)入的模塊。
import myCats myCats.cats myCats.cats[0] myCats.cats[0]['name']
以上就是python pprint.pformat()函數(shù)的使用,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
新聞名稱:創(chuàng)新互聯(lián)Python教程:pythonpprint.pformat()函數(shù)的使用
URL標(biāo)題:http://fisionsoft.com.cn/article/ccecgpj.html


咨詢
建站咨詢
