Python的pprint模块中使用的格式化可以按照一种格式正确的显示数据, 这种格式即可被解析器解析, 又很易读. 输出保存在一个单行内, 但如果有必要, 在分割多行数据时也可使用缩进表示.
1
2
3
|
import sys import pprint pprint.pprint(sys.path) |
运行结果:
1
2
3
4
5
6
7
8
|
[ '' , '/usr/local/lib/python27.zip' , '/usr/local/lib/python2.7' , '/usr/local/lib/python2.7/plat-linux2' , '/usr/local/lib/python2.7/lib-tk' , '/usr/local/lib/python2.7/lib-old' , '/usr/local/lib/python2.7/lib-dynload' , '/usr/local/lib/python2.7/site-packages' ] |
如果仅仅想获得数据而不是输出数据也可以用pformat
1
2
3
4
|
import sys import pprint str = pprint.pformat(sys.path) print str |
运行结果:
1
2
3
4
5
6
7
8
|
[ '' , '/usr/local/lib/python27.zip' , '/usr/local/lib/python2.7' , '/usr/local/lib/python2.7/plat-linux2' , '/usr/local/lib/python2.7/lib-tk' , '/usr/local/lib/python2.7/lib-old' , '/usr/local/lib/python2.7/lib-dynload' , '/usr/local/lib/python2.7/site-packages' ] |
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容