← Volver

Cómo imprimir datos organizados vía pprint()

El módulo estándar pprint incluye un conjunto de funciones para mejorar la legibilidad de los datos al momento de imprimirlos en pantalla. La función principal es pprint.pprint(), que recibe cualquier objeto de Python y lo imprime en pantalla de la forma más legible posbile. Esto es especialmente para colecciones que contengan otras colecciones. Por ejemplo:

import pprint

# Un diccionario aleatorio que contiene otros diccionarios y listas.
json = {"id":"rootNode","type":"array","valueType":"","children":[{"id":"personNode","type":"object","valueType":"","children":[{"id":"0fe6dd7-aa26-6f5b-3f8-8a150fbd8b50","type":"value","valueType":"fullName","children":[],"repeatCount":["1","100"],"fieldName":"full name","valueOptions":{}},{"id":"addressNode","type":"object","valueType":"","children":[{"id":"aa83b6a-0e8a-322d-d7d-367ff6261","type":"value","valueType":"streetAddress","children":[],"repeatCount":["1","100"],"fieldName":"street","valueOptions":{}},{"id":"f365d8d-c222-dec-b6-8ff7aaa0078","type":"value","valueType":"city","children":[],"repeatCount":["1","100"],"fieldName":"city","valueOptions":{}}],"repeatCount":["1","100"],"fieldName":"address","valueOptions":{}}],"repeatCount":["1","100"],"fieldName":"","valueOptions":{}}],"repeatCount":["1","100"],"fieldName":"","valueOptions":{}}
pprint.pprint(json)

Imprime:

{'children': [{'children': [{'children': [],
                             'fieldName': 'full name',
                             'id': '0fe6dd7-aa26-6f5b-3f8-8a150fbd8b50',
                             'repeatCount': ['1', '100'],
                             'type': 'value',
                             'valueOptions': {},
                             'valueType': 'fullName'},
                            {'children': [{'children': [],
                                           'fieldName': 'street',
                                           'id': 'aa83b6a-0e8a-322d-d7d-367ff6261',
                                           'repeatCount': ['1', '100'],
                                           'type': 'value',
                                           'valueOptions': {},
                                           'valueType': 'streetAddress'},
                                          {'children': [],
                                           'fieldName': 'city',
                                           'id': 'f365d8d-c222-dec-b6-8ff7aaa0078',
                                           'repeatCount': ['1', '100'],
                                           'type': 'value',
                                           'valueOptions': {},
                                           'valueType': 'city'}],
                             'fieldName': 'address',
                             'id': 'addressNode',
                             'repeatCount': ['1', '100'],
                             'type': 'object',
                             'valueOptions': {},
                             'valueType': ''}],
               'fieldName': '',
               'id': 'personNode',
               'repeatCount': ['1', '100'],
               'type': 'object',
               'valueOptions': {},
               'valueType': ''}],
 'fieldName': '',
 'id': 'rootNode',
 'repeatCount': ['1', '100'],
 'type': 'array',
 'valueOptions': {},
 'valueType': ''}


¿Alguna duda sobre este código? ¡Preguntanos en el foro!