python - Print dict items in alphabet order -


i'm working on python script dict channels items every time when items inserted using code:

channels = {} elem in tv_elem.getchildren():     if elem.tag == 'channel':        channels[elem.attrib['id']] = self.load_channel(elem)        channel_key in channels:            channel = channels[channel_key]            display_name = channel.get_display_name()            print display_name 

here print out:

20:58:02 t:6548  notice: bbc 1 uk en 20:58:02 t:6548  notice: svt 1 se se 20:58:02 t:6548  notice: national geographic channel uk en 20:58:02 t:6548  notice: nrk1 no no 20:58:02 t:6548  notice: discovery channel uk en 20:58:02 t:6548  notice: ard de de 20:58:02 t:6548  notice: dr1 dk dk 

here xml file:

<?xml version="1.0" encoding="utf-8" ?> <tv generator-info-name="www.timefor.tv/xmltv">         <channel id="www.timefor.tv/tv/162">             <display-name lang="de">ard de de</display-name>         </channel>         <channel id="www.timefor.tv/tv/1">             <display-name lang="dk">dr1 dk dk</display-name>         </channel>         <channel id="www.timefor.tv/tv/130">             <display-name lang="no">nrk1 no no</display-name>         </channel>         <channel id="www.timefor.tv/tv/135">             <display-name lang="se">svt 1 se se</display-name>         </channel>         <channel id="www.timefor.tv/tv/10769">             <display-name lang="en">bbc 1 uk en</display-name>         </channel>         <channel id="www.timefor.tv/tv/10214">             <display-name lang="en">national geographic channel uk en</display-name>         </channel>         <channel id="www.timefor.tv/tv/10847">             <display-name lang="en">discovery channel uk en</display-name>         </channel></tv> 

i want print them in same alphabet order xml file, have print not in same order xml file. know how can print items in same alphabet order xml file using code?

based on you've explained:

#additional import collections import ordereddict channels = ordereddict() elem in tv_elem.getchildren():     if elem.tag == 'channel':        channels[elem.attrib['id']] = self.load_channel(elem)        channel_value in channels.items():            print channel_value.get_display_name() 

note: give same order read them xml, not alphabetical

edit : since you're using python 2.6, small workaround:

channels = [] elem in tv_elem.getchildren():     if elem.tag == 'channel':        channels.append( (elem.attrib['id'], self.load_channel(elem)) )        channel_value in channels:            print channel_value[1].get_display_name() 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -