Reputation: 1040
I am working on a dict in python. I am trying to sort it out alphabetically and split it to look slightly better. Here is the code I have so far in the dictonatary.
authorentry = {'author': name, 'date': datef , 'path': path_change , 'msg' : xmlMsgf }
if not name in author:
author[ name ] = []
author[ name ].append( authorentry )
if not authorentry in author.items():
author['author'] = [authorentry]
print sorted (author.keys()), sorted (author.values())
Now what i want it to do is print out the dict in a sorted order based on the author and the date. and also to split it and modified it so it doesn't have all those commas and 'u's if that is possible. Any ideas on how to accomplish it?
this is what it looks like when i print it it as it is.
how i want is author shows up first in the list not date. And I would like it alphabetical if possible and to remove the commas in the entrys to print it out cleaner. is it possible?
[[{'date': ['06-08-2012 09:01:52 PM'], 'path': [u'/branches/Patch_4_2_0_Branch'], 'msg': ['none', u'PATCH_BRANCH:N/A\nBUG_NUMBER:N/A\nFEATURE_AFFECTED:N/A\nOVERVIEW:N/A\nAdding the SVN log size requirement to the branch \n'], 'author': u'glv'}], [{'date': ['06-08-2012 09:01:52 PM'], 'path': [u'/branches/Patch_4_2_0_Branch'], 'msg': ['none', u'PATCH_BRANCH:N/A\nBUG_NUMBER:N/A\nFEATURE_AFFECTED:N/A\nOVERVIEW:N/A\nAdding the SVN log size requirement to the branch \n'], 'author': u'glv'}]]
Update: as of right now I can group the authors together but for some reason not only can't i get it alphebetized I cannot even get the author to be the first person on the list what shows up is something similar to this:
Date: 06-08-2012 08:56:09 PM
Changes by : glv
Comments: PATCH_BRANCH:N/A BUG_NUMBER:N/A FEATURE_AFFECTED:N/A OVERVIEW:N/A Adding the svn commit line requrement
Directory Location: /trunk
The way i wanted it ordered is more like this.
Changes by : glv
Date: 06-08-2012 08:56:09 PM
Directory Location: /trunk
Comments: PATCH_BRANCH:N/A BUG_NUMBER:N/A FEATURE_AFFECTED:N/A OVERVIEW:N/A Adding the svn commit line requrement
I tried the OrderedList to see if I can get it to work that way but so far no luck or success. Is there something I am missing?
Upvotes: 3
Views: 313
Reputation: 8607
If you just care about presenting this information for user readability, use pprint
module.
import pprint
pprint.pprint(author)
assuming author
is a dict
. Alternatively use pprint.pformat
to get a string which you can further manipulate/clean up e.g. print pprint.pformat(author).replace(',','')
to remove commas.
You should also know that dict
s can not be reordered since they are essentially a hashtable whos keys are hashes (like a set).
You can also try using collections.OrdererdDict
:
from collections import OrdererdDict
sorted_author = OrderedDict(sorted(author.iteritems()))
Update: 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:
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)
Then you can do something like:
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
and get output like this:
author: glv
date: 06-08-2012 09:01:52 PM
path: /branches/Patch_4_2_0_Branch
msg: blah blah blah
Upvotes: 2
Reputation: 82889
You might consider creating a class for authorentry
instead of using a dict and implementing the __str__
method.
class authorentry:
# create authorentry; usage: x = authorentry(author, date, path, msg)
def __init__(self, author, date, path, msg):
self.author = author
self.date = date
self.path = path
self.msg = msg
# return string representation for authorentry
def __str__(self):
return "Authorentry(name: %s, date: %r, path: ...)" % (self.author, self.date, ...)
Now you can create and print an authorentry
like this:
ae = authorentry("some name", "some date", "some path", "some message")
print ae
Upvotes: 1