Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you just care about presenting this information for user readability, use <code>pprint</code> module.</p> <pre><code>import pprint pprint.pprint(author) </code></pre> <p>assuming <code>author</code> is a <code>dict</code>. Alternatively use <code>pprint.pformat</code> to get a string which you can further manipulate/clean up e.g. <code>print pprint.pformat(author).replace(',','')</code> to remove commas.</p> <p>You should also know that <code>dict</code>s can not be reordered since they are essentially a hashtable whos keys are hashes (like a set).</p> <p>You can also try using <code>collections.OrdererdDict</code>:</p> <pre><code>from collections import OrdererdDict sorted_author = OrderedDict(sorted(author.iteritems())) </code></pre> <hr> <p><strong>Update:</strong> its strange you are still having problems with this. Ill just give you some code that will definitely work and you can adapt it from there:</p> <pre><code>def format_author(author): tups = sorted(author.iteritems()) # alphabetical sorting kmaxlen = max([len(k) for k, v in tups]) # for output alignment # some custom rearrangement. if there is a 'msg' key, we want it last tupkeys = [k for k, v in tups] if 'msg' in tupkeys: msg_tup = tups.pop(tupkeys.index('msg')) tups.append(msg_tup) # append to the end # alternatively tups.insert(0, msg_tup) would insert at front output = [] for k, v in tups: # dress our values if not v: v = '' elif isinstance(v, list): if len(v) == 1: v = v[0] if len(v) == 2 and v[0] in [None, 'none', 'None']: v = v[1] v = v.strip() output.append("%s: %s" % (k.rjust(kmaxlen), v)) return "\n".join(output) </code></pre> <p>Then you can do something like:</p> <pre><code>author = {'date': ['06-08-2012 09:01:52 PM'], 'path': [u'/branches/Patch_4_2_0_Branch'], 'author': u'glv', 'msg': ['none', u'blah blah blah \n']} s = format_author(author) print s </code></pre> <p>and get output like this:</p> <pre><code>author: glv date: 06-08-2012 09:01:52 PM path: /branches/Patch_4_2_0_Branch msg: blah blah blah </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload