Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 13th, 2013, 02:13 AM
taeBaby taeBaby is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 27 taeBaby User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
Xml parsing

So I need to parse an xml file using the ElementTree module and load all the data into a list of tuples which should look something like this:

Code:
[(1, 'Customer#000000001', 'IVhzIApeRb ot,c,E', 15, '25-989-741-2988', 711.56, 'BUILDING', 'regular, regular platelets are fluffily according to the even attainments. blithely iron'), (2, 'Customer#000000002', 'XSTf4,NCwDVaWNe6tEgvwfmRchLXak', 13, '23-768-687-3665', 121.65, 'AUTOMOBILE', 'furiously special deposits solve slyly. furiously even foxes wake alongside of the furiously ironic ideas. pending'), ...]


Right now, I can only get it to output as this:
Code:
1 
Customer#000000001
IVhzIApeRb ot,c,E
15
25-989-741-2988
711.56
BUILDING
regular
regular platelets are fluffily according to the even attainments. blithely iron
2
Customer#000000002
XSTf4,NCwDVaWNe6tEgvwfmRchLXak
13
23-768-687-3665
121.65
AUTOMOBILE
furiously special deposits solve slyly. furiously even foxes wake alongside of the furiously ironic ideas. pending

This is my code, I'm not sure if I am suppose to use append or not because when I tried, I kept getting an error.
Code:
import xml.etree.ElementTree as ET

tree = ET.parse('customer.xml')
root = tree.getroot()

for elem in root.findall('T'):
    for i in elem.getchildren():
        test = i.text
        print test

This is the first 3 customer info (total 1500) from the xml file:
Code:
<table ID="customer">
  <T>
    <C_CUSTKEY>1</C_CUSTKEY>
    <C_NAME>Customer#000000001</C_NAME>
    <C_ADDRESS>IVhzIApeRb ot,c,E</C_ADDRESS>
    <C_NATIONKEY>15</C_NATIONKEY>
    <C_PHONE>25-989-741-2988</C_PHONE>
    <C_ACCTBAL>711.56</C_ACCTBAL>
    <C_MKTSEGMENT>BUILDING</C_MKTSEGMENT>
    <C_COMMENT>regular, regular platelets are fluffily according to the even attainments. blithely iron</C_COMMENT>
  </T>
  <T>
    <C_CUSTKEY>2</C_CUSTKEY>
    <C_NAME>Customer#000000002</C_NAME>
    <C_ADDRESS>XSTf4,NCwDVaWNe6tEgvwfmRchLXak</C_ADDRESS>
    <C_NATIONKEY>13</C_NATIONKEY>
    <C_PHONE>23-768-687-3665</C_PHONE>
    <C_ACCTBAL>121.65</C_ACCTBAL>
    <C_MKTSEGMENT>AUTOMOBILE</C_MKTSEGMENT>
    <C_COMMENT>furiously special deposits solve slyly. furiously even foxes wake alongside of the furiously ironic ideas. pending</C_COMMENT>
  </T>
  <T>
    <C_CUSTKEY>3</C_CUSTKEY>
    <C_NAME>Customer#000000003</C_NAME>
    <C_ADDRESS>MG9kdTD2WBHm</C_ADDRESS>
    <C_NATIONKEY>1</C_NATIONKEY>
    <C_PHONE>11-719-748-3364</C_PHONE>
    <C_ACCTBAL>7498.12</C_ACCTBAL>
    <C_MKTSEGMENT>AUTOMOBILE</C_MKTSEGMENT>
    <C_COMMENT>special packages wake. slyly reg</C_COMMENT>
  </T>

Reply With Quote
  #2  
Old February 13th, 2013, 09:42 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,380 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 7 m 19 sec
Reputation Power: 383
Who keeps giving you all these silly xml projects?

Why do your programming attempts all look so similarly lame?
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old February 13th, 2013, 10:40 AM
taeBaby taeBaby is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 27 taeBaby User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
Well sorry if you think these are silly xml projects and that my programming attempts are all similarly lame.
I already know I'm slower than the average person, so thanks for pointing it out.

Reply With Quote
  #4  
Old February 13th, 2013, 11:37 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,380 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 7 m 19 sec
Reputation Power: 383
Well, at first I thought you were employed by a record store. I'm glad to help a little bit. In that case, you're employed and I'm not employed, so I'm not happy to help a lot.

Next I thought you are a student because I'm sure I saw
#fill in the rest of the function
in a few of the routines. Doing homework for other people is an ethics violation. Although, again, I err on the side of answering homework questions because examples of working code seem better than examples of bad algorithms. And since I write doctests into many of my posts, and try them before I post, for the most part I'm posting somewhat functional algorithms. Sometimes I understand the questions and answer accordingly.

Your xml isn't always about audio recordings. Maybe you're not a music store employee. The xml questions have been long running and have not changed much. It seems like a class would advance faster. So perhaps you're not a student, either. I'm baffled. But I also don't feel like answering more xml questions. I've now explored the xml module and have learned enough about it that I've lost personal motivation to answer more of these questions.

Last edited by b49P23TIvg : February 13th, 2013 at 12:06 PM.

Reply With Quote
  #5  
Old February 13th, 2013, 11:39 AM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 483 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 51 m 26 sec
Reputation Power: 63
So you want a list of tuples ...
Code:
''' xml_test107.py

'''

import xml.etree.ElementTree as ET

tree = ET.parse('customer.xml')
root = tree.getroot()

mylist = []
for elem in root.findall('T'):
    sublist = []
    for ix, line in enumerate(elem.getchildren()):
        test = line.text
        sublist.append(test)
        if ix % 8 == 7:
             mylist.append(tuple(sublist))
             sublist = []


import pprint
pprint.pprint(mylist)

''' result ...

[('1',
  'Customer#000000001',
  'IVhzIApeRb ot,c,E',
  '15',
  '25-989-741-2988',
  '711.56',
  'BUILDING',
  'regular, regular platelets are fluffily according to the even attainments. blithely iron'),
 ('2',
  'Customer#000000002',
  'XSTf4,NCwDVaWNe6tEgvwfmRchLXak',
  '13',
  '23-768-687-3665',
  '121.65',
  'AUTOMOBILE',
  'furiously special deposits solve slyly. furiously even foxes wake alongside of the furiously ironic ideas. pending'),
 ('3',
  'Customer#000000003',
  'MG9kdTD2WBHm',
  '1',
  '11-719-748-3364',
  '7498.12',
  'AUTOMOBILE',
  'special packages wake. slyly reg')]

'''
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Xml parsing

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap