E
E
Evgeny Rymarev2011-04-16 20:02:57
Python
Evgeny Rymarev, 2011-04-16 20:02:57

How to convert a Python dictionary to human readable form?

There is a dictionary like this:

{'fourbluepair': {'lect': '\xd0\x9c\xd0\xbe\xd1\x81\xd1\x83\xd0\xbd\xd0\xbe\xd0\xb2\xd0\xb0 \xd0\x9e.\xd0\x93.', 'cab': '0-416', 'name': '\xd0\xad\xd0\xba\xd0\xbe\xd0\xbd. \xd0\xbf\xd1\x80\xd0\xb5\xd0\xb4\xd0\xbf\xd1\x80\xd0\xb8\xd1\x8f\xd1\x82\xd0\xb8\xd1\x8f'}, 'threeredpair': {'lect': '\xd0\x91\xd0\xbe\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd0\xb8\xd0\xbd\xd0\xb0 \xd0\x95.\xd0\x92.', 'cab': '0-218', 'name': '\xd0\xa2\xd0\xb5\xd0\xbe\xd1\x80\xd0\xb8\xd1\x8f \xd1\x8d\xd0\xba\xd0\xbe\xd0\xbd. \xd0\x98\xd0\xa2'}, 'fourredpair': {'lect': '\xd0\xa0\xd1\x83\xd1\x82\xd0\xba\xd0\xb0\xd1\x83\xd1\x81\xd0\xba\xd0\xb0\xd1\x81 \xd0\xa2.\xd0\x9a.', 'cab': '0-321', 'name': '\xd0\xad\xd0\xba\xd0\xbe\xd0\xbd. \xd0\xbf\xd1\x80\xd0\xb5\xd0\xb4\xd0\xbf\xd1\x80\xd0\xb8\xd1\x8f\xd1\x82\xd0\xb8\xd1\x8f'}, 'threebluepair': {'lect': '\xd0\x91\xd0\xbe\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd0\xb8\xd0\xbd\xd0\xb0 \xd0\x95.\xd0\x92.', 'cab': '0-218', 'name': '\xd0\xa2\xd0\xb5\xd0\xbe\xd1\x80\xd0\xb8\xd1\x8f \xd1\x8d\xd0\xba\xd0\xbe\xd0\xbd. \xd0\x98\xd0\xa2'}, 'day_of_week': u'\u041f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a'}

you need to get it in a human-readable form, that is:
{<br/>
 'fourbluepair':<br/>
 {<br/>
 'lect': '\xd0\x9c\xd0\xbe\xd1\x81\xd1\x83\xd0\xbd\xd0\xbe\xd0\xb2\xd0\xb0 \xd0\x9e.\xd0\x93.',<br/>
 'cab': '0-416',<br/>
 'name': '\xd0\xad\xd0\xba\xd0\xbe\xd0\xbd. \xd0\xbf\xd1\x80\xd0\xb5\xd0\xb4\xd0\xbf\xd1\x80\xd0\xb8\xd1\x8f\xd1\x82\xd0\xb8\xd1\x8f'<br/>
 },<br/>
 'threeredpair':<br/>
 {<br/>
 'lect': '\xd0\x91\xd0\xbe\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd0\xb8\xd0\xbd\xd0\xb0 \xd0\x95.\xd0\x92.',<br/>
 'cab': '0-218',<br/>
 'name': '\xd0\xa2\xd0\xb5\xd0\xbe\xd1\x80\xd0\xb8\xd1\x8f \xd1\x8d\xd0\xba\xd0\xbe\xd0\xbd. \xd0\x98\xd0\xa2'<br/>
 },<br/>
 'fourredpair':<br/>
 {<br/>
 'lect': '\xd0\xa0\xd1\x83\xd1\x82\xd0\xba\xd0\xb0\xd1\x83\xd1\x81\xd0\xba\xd0\xb0\xd1\x81 \xd0\xa2.\xd0\x9a.',<br/>
 'cab': '0-321',<br/>
 'name': '\xd0\xad\xd0\xba\xd0\xbe\xd0\xbd. \xd0\xbf\xd1\x80\xd0\xb5\xd0\xb4\xd0\xbf\xd1\x80\xd0\xb8\xd1\x8f\xd1\x82\xd0\xb8\xd1\x8f'<br/>
 },<br/>
 'threebluepair':<br/>
 {<br/>
 'lect': '\xd0\x91\xd0\xbe\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd0\xb8\xd0\xbd\xd0\xb0 \xd0\x95.\xd0\x92.',<br/>
 'cab': '0-218',<br/>
 'name': '\xd0\xa2\xd0\xb5\xd0\xbe\xd1\x80\xd0\xb8\xd1\x8f \xd1\x8d\xd0\xba\xd0\xbe\xd0\xbd. \xd0\x98\xd0\xa2'<br/>
 },<br/>
 'day_of_week': u'\u041f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a'<br/>
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
MikhailEdoshin, 2011-04-16
@Eugene_Rymarev

from pprint import pprint
pprint(mydict)

I
Imbolc, 2011-04-21
@Imbolc

import simplejson as json

def pp(obj, *args, **kwargs):
    '''pprint с раскодированым выводом русских строк'''
    def default(v):
        try:
            ret = str(v)
        except:
            ret = 'not json serializable'
        if len(ret) > 50:
            ret = ret[:50] + ' ...'
        return ret
    print json.dumps(obj, ensure_ascii=False, indent=4,
        default=default).encode('utf-8')

N
Nostromo, 2011-04-17
@Nostromo

I’ll add an even more ridiculous question - is it possible to display it not in Unicode, but in a readable form for a person when printing a dictionary (as I understand it, in str representation)?
I use Python only as an application tool, it would be very helpful in logging and some calculations.

B
Bteam, 2011-04-17
@Bteam

Use ipython,
In[1]: a = {'a':1}
In[2]: a
Out[2]: {'a': 1}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question