[ { 'domain': '.cnsuning.com', 'httpOnly': False, 'name': 'userBeanToken', 'path': '/', 'secure': False, 'value': '74856332-4b37-4bf1-82f7-54dbfbb24be4'}, { 'domain': 'fund.cnsuning.com', 'httpOnly': False, 'name': 'route', 'path': '/', 'secure': False, 'value': '1cf85971f30b5f64579b97a6da006700'}, { 'domain': 'fund.cnsuning.com', 'httpOnly': False, 'name': 'JSESSIONID', 'path': '/snweb_portal', 'secure': False, 'value': 'lMi0YeDeEdzBQ37eQjUmMP8P.fmsysprdapp201'}, { 'domain': '.cnsuning.com', 'expiry': 1569230440.361169, 'httpOnly': True, 'name': 'idsLoginUserIdLastTime', 'path': '/', 'secure': False, 'value': '15071717'}, { 'domain': '.cnsuning.com', 'httpOnly': True, 'name': 'authId', 'path': '/', 'secure': False, 'value': 'si5B7380F42988E719497564743DD6855F'}, { 'domain': '.cnsuning.com', 'httpOnly': False, 'name': 'userCode', 'path': '/', 'secure': False, 'value': '15071717'}]
pprint方法概览
美化输出
我们可以利用pprint中的PrettyPrinter控制输出的打印时的缩进,行宽、甚至打印深度等,定义类如下class pprint.PrettyPrinter(indent = 1,width = 80,depth = None,stream = None,*,compact = False )
indent 缩进width 宽度depth 打印深度stream 指输出流对象,stream = None输出流对象默认是sys.stdoutcompact 如果compact为false(默认值),则长序列中的每个项目将在单独的行上进行格式化。如果compact为true,则将在每个输出行上格式化适合宽度的项目。 import pprint L = [str(i)*20 for i in range(10)] pp = pprint.PrettyPrinter(indent=4) pp.pprint(L) print(L)示例结果:
[ '00000000000000000000', '11111111111111111111', '22222222222222222222', '33333333333333333333', '44444444444444444444', '55555555555555555555', '66666666666666666666', '77777777777777777777', '88888888888888888888', '99999999999999999999']['00000000000000000000', '11111111111111111111', '22222222222222222222', '33333333333333333333', '44444444444444444444', '55555555555555555555', '66666666666666666666', '77777777777777777777', '88888888888888888888', '99999999999999999999']
对象字符串
我们也可以将目标对象的格式化表示形式返回为字符串。 indent, width,depth和compact将PrettyPrinter 作为格式化参数传递给构造函数,定义类如下
L = [str(i)*20 for i in range(10)] pp = pprint.pformat(L, indent=4) print(pp) print(L)示例结果:
[ '00000000000000000000', '11111111111111111111', '22222222222222222222', '33333333333333333333', '44444444444444444444', '55555555555555555555', '66666666666666666666', '77777777777777777777', '88888888888888888888', '99999999999999999999']['00000000000000000000', '11111111111111111111', '22222222222222222222', '33333333333333333333', '44444444444444444444', '55555555555555555555', '66666666666666666666', '77777777777777777777', '88888888888888888888', '99999999999999999999']
格式化打印
输出格式的对象字符串到指定的输出流,最后以换行符结束,定义类如下
import pprint L = [str(i)*20 for i in range(10)] pprint.pprint(L, indent=4) print(L)示例结果:
[ '00000000000000000000', '11111111111111111111', '22222222222222222222', '33333333333333333333', '44444444444444444444', '55555555555555555555', '66666666666666666666', '77777777777777777777', '88888888888888888888', '99999999999999999999']['00000000000000000000', '11111111111111111111', '22222222222222222222', '33333333333333333333', '44444444444444444444', '55555555555555555555', '66666666666666666666', '77777777777777777777', '88888888888888888888', '99999999999999999999']
可读性
判断对象object的字符串对象是否可读,True可读,反之则反。
import pprint L = [str(i)*20 for i in range(10)] B = pprint.isreadable(L) print(B)示例结果:
True
转载于:https://www.cnblogs.com/jessitommy/p/11075862.html
相关资源:Python中使用pprint函数进行格式化输出的教程