首页 > 解决方案 > 使用 Class 调用解析 XML

问题描述

xml.etree我用python 模块解析了一个 xml 文件。它运行良好,但现在我尝试将此代码称为主程序中的模块/类。我想将 xml 树和用于 csv 写入类的文件名发送。

我的虚拟文件用类调用文件:

import xml.etree.ElementTree as ET
tree = ET.ElementTree(file='phonebook.xml')
root = tree.getroot()
from xml2csv import Vcard_xml2csv

my_export = Vcard_xml2csv(tree, 'phonebook.csv')
my_export.write_csv()

这是课程:

class Vcard_xml2csv:
    """The XML phone book export to a csv file"""
    
    def __init__(self, tree, csvfilename):
        root = tree.getroot()
        self.csvfilename = csvfilename    

    def write_content(contact, csvfilename):
        with open(csvfilename, mode='a+') as phonebook_file:
            contact_writer = csv.writer(phonebook_file, delimiter=',', quotechar=" ", quoting=csv.QUOTE_MINIMAL)
            contact_writer.writerow([contact]) # delete [] if you will see only text separated by a comma 

    def write_csv(tree):
        for contacts in tree.iter(tag='phonebook'):
            for contact in contacts.findall("./contact"):       
                row=[]
                for category in contact.findall("./category"):
                    print('Category: ',category.text)
                    category=category.text
                    row.append(category)
                    
                   
                for person in contact.findall("./person/realName"):
                    print('realName: ',person.text)
                    realName=person.text
                    row.append(realName)
                
                for mod_time in contact.findall("./mod_time"):
                    print ('mod_time: ', mod_time.text)
                    mod_time=mod_time.text
                    row.append(mod_time)

                
                for uniqueid in contact.findall("./uniqueid"):
                    print ('uniqueid: ', uniqueid.text)
                    uniqueid_=uniqueid.text
                    row.append(uniqueid_)
                    
                    
                numberlist=[]    
                for number in contact.findall("./telephony/number"):
                    
                    print('id',number.attrib['id'],'type:',number.attrib['type'], 'prio:',number.attrib['prio'], 'number: ',number.text)
                    id_=number.attrib['id']
                    numberlist.append(id_)
                    type_=number.attrib['type']
                    numberlist.append(type_)
                    prio_=number.attrib['prio']
                    numberlist.append(prio_)
                    number_=number.text
                    numberlist.append(number_)
                    
                    contact = row + numberlist            
                    write_content(contact, csvfilename)   
                    numberlist=[]                         

我收到以下错误:

对于 tree.iter(tag='phonebook') 中的联系人:AttributeError: 'Vcard_xml2csv' object has no attribute 'iter' 感谢您的帮助!

标签: python-3.xxmlclass

解决方案


当我们在类中定义方法时,就像write_csv()在示例中一样,第一个参数始终是类实例。将其视为访问类属性和方法的一种方式。按照惯例,为了便于阅读,第一个参数称为self.

在 write_csv 方法中,tree已成为此类实例,这就是您看到错误的原因。对此的解决方案是定义如下方法:

def write_csv(self, tree)
    ....
    ....

并且对该方法的调用将是:

my_export.write_csv(tree)

我希望这有帮助。更多关于self 这里


推荐阅读