将Parser元素转换为Class Object

时间:2013-06-12 09:39:33

标签: python parsing object

我已经尝试了以下代码将解析器元素转换为类对象 但它没有正常工作.....希望有人可以帮我解决它......

import xml.etree.ElementTree as ET 
tree = ET.parse ( 'country.xml' )
class country:
    def  _init_(self,name,rank,gdppc,neighbor):
        self.name = name
        self.rank = rank
        self.gdppc = gdppc
        self.neighbor = neighbor
    def __str__(self):
        return " Country :Name : %s, Rank : %s ,GDDPC: ,Neighbor:" % (self.name, self.rank, self.gdppc ,self.neighbor)    
class state:
    def _init_(self,name,city):
        self.name =name
        self.city =city

walkAll = tree.getiterator()
listy = [[] for i in range(3)]
choice = raw_input('enter your choice (country/state):')
for  elt in walkAll:
     if elt.tag == choice:               
        for et in elt:
            c    = et.attrib
            print str(c)

而不是调用 str ()int只是以正常方式打印属性...我想将这些属性移动到连续的对象数据成员.....

0 个答案:

没有答案