2

I'm working on my python script to dict the channels items every time when the items is inserted by using this code:

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

Here is what it print out:

20:58:02 T:6548  NOTICE: BBC One 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 is the 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 One 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 to print them in the same alphabet order as the XML file, what I have print it is not in the same order as the XML file. Do you know how I can print the items in the same alphabet order as the XML file using my code?

  • 2
    A `dict` is essentially unordered. Try using an [OrderedDict](http://docs.python.org/2/library/collections.html#collections.OrderedDict) – Tejas Pendse Mar 10 '14 at 16:27
  • It doesn't seem that the xml file is sorted alphabetically (either by `tv/xxxxx` or `display-name`. Is this intended? – wflynny Mar 10 '14 at 16:27
  • @mogambo thank you for your advice. Could you please post the source of the OrderedDict with my code that I should use? –  Mar 10 '14 at 16:30
  • Do you want the same ordering as the XML, or do you want to sort the channels by display name? – Steinar Lima Mar 10 '14 at 16:30
  • And should `for channel in channels` be inside the other `for` loop? It doesn't make any sense. – Steinar Lima Mar 10 '14 at 16:31
  • You could just use a list if order rather than lookup is important – EdChum Mar 10 '14 at 16:32
  • @SteinarLima yes I'd want the same ordering as the XML, but if that is not possible then listing the channels in OrderedDict would be great. –  Mar 10 '14 at 16:33

2 Answers2

3

Based on what you've explained:

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

NOTE: This will give you the same order as you read them from the XML, not alphabetical

EDIT : Since you're using Python 2.6, a small workaround:

channels = []
for elem in tv_elem.getchildren():
    if elem.tag == 'channel':
       channels.append( (elem.attrib['id'], self.load_channel(elem)) )
       for channel_value in channels:
           print channel_value[1].get_display_name()
Tejas Pendse
  • 551
  • 6
  • 19
  • Thanks for this, but I have got a problem. I have got an error `from collections import OrderedDict ImportError: cannot import name OrderedDict.` I don't know why I have got an error? I'm using python 2.6 version for xbmc. –  Mar 10 '14 at 16:47
  • Unfortunately, `OrderedDict` was introduced in Python 2.7. I guess you'll have to use nested lists. – Tejas Pendse Mar 10 '14 at 16:50
  • 1
    OrderedDict was added to the `collections` module in Python 2.7. http://docs.python.org/2/library/collections.html#collections.OrderedDict Use this recipe if you're using Python 2.6: http://code.activestate.com/recipes/576693/ – IceArdor Mar 10 '14 at 16:50
  • @IceArdor Thank you very much for your help. It have helping me to solve the problem. I have got the list of items that are in perfect alphabet order as the XML file. –  Mar 10 '14 at 17:33
1

Update: mogambo got in first while I was typing :)

Replace first line with

from collections import OrderedDict
channels = OrderedDict()

I would only add: you might just as well append your channel objects to a list, if you never need to retrieve them individually by id and only want to iterate over them in the same order as they appeared in the XML.

scav
  • 1,095
  • 1
  • 9
  • 17
  • scav is correct in stating that if you don't need constant-time lookup to an individual item in your dictionary then you might as well use a list, which is more memory efficient. – IceArdor Mar 10 '14 at 17:53