Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Sorting a list created from a parsed xml message

Reply
Thread Tools

Sorting a list created from a parsed xml message

 
 
kaklis@gmail.com
Guest
Posts: n/a
 
      07-21-2010
Hi pythonistas,
From the subject of my message it's clear that i get an xml message
from a socket, 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


What i want is to print this list sorted by ID_Col?
Any Suggestions?

Antonis K.
 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      07-21-2010
http://www.velocityreviews.com/forums/(E-Mail Removed), 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

 
Reply With Quote
 
 
 
 
kaklis@gmail.com
Guest
Posts: n/a
 
      07-21-2010
On Jul 21, 8:58*am, Stefan Behnel <(E-Mail Removed)> wrote:
> (E-Mail Removed), 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.
 
Reply With Quote
 
kaklis@gmail.com
Guest
Posts: n/a
 
      07-21-2010
On Jul 21, 9:04*am, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> On Jul 21, 8:58*am, Stefan Behnel <(E-Mail Removed)> wrote:
>
>
>
> > (E-Mail Removed), 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
 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      07-21-2010
(E-Mail Removed), 21.07.2010 15:38:
> On Jul 21, 9:04 am, "(E-Mail Removed)"<(E-Mail Removed)> wrote:
>> On Jul 21, 8:58 am, Stefan Behnel<(E-Mail Removed)> wrote:
>>
>>
>>
>>> (E-Mail Removed), 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')


It seems you want to sort the list numerically. In that case, use
int(SortedServers[0]) as the key. Sorting by string values will sort the
list lexicographically.


> How do i remove the u'


You should read the Python tutorial, specifically the sections about
strings. Then, read the sections on lists and sequences.

In short: Don't care about the "u'" prefix, that's just fine.

Stefan

 
Reply With Quote
 
Thomas Jollans
Guest
Posts: n/a
 
      07-22-2010
On 07/21/2010 03:38 PM, (E-Mail Removed) wrote:
> On Jul 21, 9:04 am, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>> On Jul 21, 8:58 am, Stefan Behnel <(E-Mail Removed)> wrote:
>>
>>
>>
>>> (E-Mail Removed), 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")


Please try to persuade your mail client to not mess up python code, if
you could. It would make this *so* much easier to read

>
> SortedServers.append(tempTuple)
>
> Sorted = sorted(SortedServers, key=lambda id: SortedServers[0])


Anyway, let's look at that key function of yours:

key=lambda id: SortedServers[0]

translated to traditional function syntax:

def key(id):
return SortedServers[0]

No matter which item sorted() examines, the key it sorts by is always
the same (the first item of the WHOLE LIST).
You want something more like this:

def key(row):
return row[0]

ergo, what you want, all in all, is either of these:

Sorted = sorted(SortedServers, key=(lambda row: row[0])) # option 1
SortedServers.sort(key=(lambda row: row[0])) # option 2

option 2, the in-place sort, might be faster.

(and, as Stefan noted, as you probably want a numeric sort, you'll want
your key to be an int)


> 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


 
Reply With Quote
 
kaklis@gmail.com
Guest
Posts: n/a
 
      07-27-2010
On Jul 22, 12:56*pm, Thomas Jollans <(E-Mail Removed)> wrote:
> On 07/21/2010 03:38 PM, (E-Mail Removed) wrote:
>
>
>
> > On Jul 21, 9:04 am, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> >> On Jul 21, 8:58 am, Stefan Behnel <(E-Mail Removed)> wrote:

>
> >>> (E-Mail Removed), 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")

>
> Please try to persuade your mail client to not mess up python code, if
> you could. It would make this *so* much easier to read
>
>
>
> > * * * * * * * SortedServers.append(tempTuple)

>
> > Sorted = sorted(SortedServers, key=lambda id: SortedServers[0])

>
> Anyway, let's look at that key function of yours:
>
> key=lambda id: SortedServers[0]
>
> translated to traditional function syntax:
>
> def key(id):
> * * return SortedServers[0]
>
> No matter which item sorted() examines, the key it sorts by is always
> the same (the first item of the WHOLE LIST).
> You want something more like this:
>
> def key(row):
> * * return row[0]
>
> ergo, what you want, all in all, is either of these:
>
> Sorted = sorted(SortedServers, key=(lambda row: row[0])) # option 1
> SortedServers.sort(key=(lambda row: row[0])) * * * * * * # option 2
>
> option 2, the in-place sort, might be faster.
>
> (and, as Stefan noted, as you probably want a numeric sort, you'll want
> your key to be an int)
>
> > 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

>
>


Thank you so much for your detailed response!

Antonis K.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java and huge XML file to be parsed Katrin Tomanek Java 47 04-15-2006 04:13 AM
Validating parsed XML document against XML-schema TKok Java 1 12-08-2005 02:01 PM
insertion of string into characters of parsed xml (in SAX) YuliaG Java 2 04-04-2005 07:16 AM
What is the best way to create C++ objects from parsed XML data? Jim Williams XML 2 08-08-2004 08:28 AM
How to embed html in xml (i.e. prevent the html from being parsed)? Failure XML 1 09-07-2003 09:34 PM



Advertisments