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']
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试