0

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

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 只是以正常方式打印属性......我想将这些属性移动到连续的对象数据成员......

4

0 回答 0