5 Replies - 256 Views - Last Post: 22 August 2014 - 01:10 PM Rate Topic: -----

#1 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-December 09

Sorting after parsing an XML

Posted 22 August 2014 - 11:17 AM

Hello, I need direction on a little script I have going here.
The background is several times a month I will be grabbing an XML file with a tremendous amount of information. I only need 2 items from the XML, Last Name and Code number. I've written my XML parser which seems to work right now. It displays all the information I need but I cannot figure out the sorting.

If I get it sorted, it places the numerical codes first but it seems to only check the first digit so no real sorting happens. I really wanted it sorted by Last name which there could be several of the same last name with different codes next to them.

I've tried Google, maybe I'm tired and can't tighten up by search criteria but I keep getting results for numerical sorting when I want alphabetical first with the numerical next to it.
ie:

12:3-456 Smith
24:6-987 Smith
12:3-654 Jones

What I want is:
Jones 12:3-654
Smith 12:3-456
Smith 24:6-987


import xml.etree.ElementTree as etree
import os

#define data file
data_file = 'ticketsexport.xml'
	
xmlD = etree.parse(data_file)

#Loops through all tickets and only prints Last Name and Code number.
for ticket in xmlD.iter('Ticket'):
	lname = ticket.find('LastName').text
	code = ticket.find('Code').text
	print lname, code
	
	




Is This A Good Question/Topic? 0
  • +

Replies To: Sorting after parsing an XML

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: Sorting after parsing an XML

Posted 22 August 2014 - 11:31 AM

Quote

If I get it sorted, it places the numerical codes first ..


Where is your attempt to sort?

Also, a copy or cut-down version of the xml-file would be useful.

This post has been edited by andrewsw: 22 August 2014 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-December 09

Re: Sorting after parsing an XML

Posted 22 August 2014 - 11:48 AM

Sorry, I took it out as I was getting frustrated.
Here is the code again and an XML clip
import xml.etree.ElementTree as etree
import os

#define data file
data_file = 'ticketsexport.xml'
	
xmlD = etree.parse(data_file)

#Loops through all tickets and only prints Last Name and Code number.
for ticket in xmlD.iter('Ticket'):
	
	lname = ticket.find('LastName').text
	code= ticket.find('Code').text
	results = [lname, code]
	results.sort()
	print results
	



<Tickets>

	<Ticket>
		<LastName>SMITH</LastName>
		<Code>12:3-456</Code>
	</Ticket>
        <Ticket>
		<LastName>SMITH</LastName>
		<Code>24:6-987</Code>
	</Ticket>
	
</Tickets



Hope I did that right.

This post has been edited by Justin3085: 22 August 2014 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: Sorting after parsing an XML

Posted 22 August 2014 - 12:02 PM

A very quick search finds this result:

http://effbot.org/zo...lement-sort.htm
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: Sorting after parsing an XML

Posted 22 August 2014 - 12:16 PM

Spoiler

This post has been edited by andrewsw: 22 August 2014 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#6 Justin3085  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 20-December 09

Re: Sorting after parsing an XML

Posted 22 August 2014 - 01:10 PM

A 'very quick' search for YOU found the result. For me, I didn't know what to really search for being a rookie and out of all the results I did look at. Your post is not one I found. So I appreciate your knowledge and Thank you for the information.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1