Sorting a list created from a parsed xml message

kaklis at gmail.com kaklis at gmail.com
Wed Jul 21 09:38:26 EDT 2010


On Jul 21, 9:04 am, "kak... at gmail.com" <kak... at gmail.com> wrote:
> On Jul 21, 8:58 am, Stefan Behnel <stefan... at behnel.de> wrote:
>
>
>
> > kak... at gmail.com, 21.07.2010 14:36:
>
> > > From the subject of my message it's clear that i get an xml message
> > > from a socket,
>
> > Not at all, but now that you say it...
>
> > > i parse it and the result is a list like the one that
> > > follows:
> > > ID_Col
> > > 4    Server        ak              ip      OFFLINE
>
> > > 29      Server     and2    ip      OFFLINE
>
> > > 5    Proxy         l34e         ip OFFLINE
>
> > > 6            Proxy         barc            ip      ONLINE
>
> > > 41           Proxy         proxy-2         ip      ONLINE
>
> > > 53           Server        server-4        ip      ONLINE
>
> > > 52           Server        server-3        ip      ONLINE
>
> > Doesn't look like a Python list to me...
>
> > > What i want is to print this list sorted by ID_Col?
> > > Any Suggestions?
>
> > Assuming that the above is supposed to represent a list of tuples, you can
> > use the .sort() method on the list and pass operator.itemgetter(0) as 'key'
> > argument (see the sort() method and the operator module).
>
> > Stefan
>
> No it is not a Python list at all. This the way i print the parsed
> items 'like a list'.
> But i want them to be sorted.

Well i did this:

SortedServers = []

for session in sessions:
    for IP in session.getElementsByTagName("ipAddress"):
         for iphn in session.getElementsByTagName("hostName"):
              tempTuple = session.getAttribute("id"),
session.getAttribute("type"), iphn.childNodes[0].data,
IP.childNodes[0].data, session.getAttribute("status")

              SortedServers.append(tempTuple)

Sorted = sorted(SortedServers, key=lambda id: SortedServers[0])
for item in Sorted:
     print item

but the list is still unsorted and with u' in front of each item

(u'4', u'Server', u'aika74', u'ip', u'OFFLINE')
(u'29', u'Server', u'ando', u'ip2', u'OFFLINE')

How do i remove the u'

Antonis



More information about the Python-list mailing list