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?
If you just care about presenting this information for user readability, use
pprintmodule.assuming
authoris adict. Alternatively usepprint.pformatto 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
dicts can not be reordered since they are essentially a hashtable whos keys are hashes (like a set).You can also try using
collections.OrdererdDict: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:
Then you can do something like:
and get output like this: