Python:修改XML文件 [英] Python: Modifying an XML File

查看:89
本文介绍了Python:修改XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困住了,我写了一段代码来查找xml文件中的特定索引.但是,当找到该索引时,不会仅使用该索引和常量参数来创建一个新的xml文件. 它返回错误:

I'm stuck, I've written a code that looks for specific index in xml file. But when find that Index won't create me a new xml file with just that Index in and constant parameters. it returns an error:

...rba_u_xml.py", line 29, in <module>
ObjectDictionary.remove(Variable)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 337, in remove
self._children.remove(element)
ValueError: list.remove(x): x not in list

这是我的代码:

import xml.etree.ElementTree as ET
tree = ET.parse('master.xml')
root = tree.getroot()

s = input('Insert a number of index and add quotes(") befor and after: ')
i = int(s, 16)

for MagicNumber in root.findall('MagicNumber'):   #constant parameters
    print MagicNumber.tag,':', MagicNumber.text
    print('\n')
for FileInfo in root.find('FileInfo'):
    print FileInfo.tag,':', FileInfo.text
print''
for DeviceInfo in root.find('DeviceInfo'):
    print DeviceInfo.tag,':', DeviceInfo.text
print''
for DummyUsage in root.find('DummyUsage'):
    print DummyUsage.tag,':', DummyUsage.text
print''
for ObjectDictionary in root.find('ObjectDictionary'):
    name=ObjectDictionary.get('name')
    print ObjectDictionary.tag,':', ObjectDictionary.text, name
    print''                                                       #constant parameters

    for Variable in ObjectDictionary.iter('Variable'):          #searching for Variables in index
        if Variable.find('./Index').text == str(i):
            print 'Variable name: ', Variable.attrib['name']
        elif Variable.find('./Index').text != str(i):
            ObjectDictionary.remove(Variable)

    for Record in ObjectDictionary.iter('Record'):             #searching for Records in index
        if Record.find('./Index').text == str(i):
            print 'Record name: ', Record.attrib['name']
        elif Record.find('./Index').text != str(i):
            ObjectDictionary.remove(Record)

    for Array in ObjectDictionary.iter('Array'):               #searching for Arrays in index
        if Array.find('./Index').text == str(i):
            print 'Array name: ', Array.attrib['name']
        elif Array.find('./Index').text != str(i):
            ObjectDictionary.remove(Array)


tree.write('output.xml')            #That would be amended to create a new xml file

感谢您的帮助! 我希望你能理解(在另一种情况下要问),因为我经常使用Google翻译:)

Thanks for your help! I hope you understand(in the other case ask) because I use google translator a lot :)

推荐答案

我解决了! :D插入xml.etree.ElementTree模块我使用xml模块,插入ObjectDictionary.remove(Variable)我使用Variable.clear()

I solved it! :D Insted of xml.etree.ElementTree modul I use xml module and insted of ObjectDictionary.remove(Variable) I use Variable.clear()

我使用lxml模块

from lxml import etree as ET
tree = ET.parse('master.xml')
root = tree.getroot()

-

s = input('Insert a number of index and add quotes(") befor and after: ')
i = int(s, 16)

for MagicNumber in root.findall('MagicNumber'):   #constant parameters
    print MagicNumber.tag,':', MagicNumber.text
    print('\n')
for FileInfo in root.find('FileInfo'):
    print FileInfo.tag,':', FileInfo.text
print''
for DeviceInfo in root.find('DeviceInfo'):
    print DeviceInfo.tag,':', DeviceInfo.text
print''
for DummyUsage in root.find('DummyUsage'):
    print DummyUsage.tag,':', DummyUsage.text
print''
for ObjectDictionary in root.find('ObjectDictionary'):
    name=ObjectDictionary.get('name')
    print ObjectDictionary.tag,':', ObjectDictionary.text, name
    print''                                                       #constant parameters

    for Variable in ObjectDictionary.iter('Variable'):          #searching for Variables in index
        if Variable.find('./Index').text == str(i):
            print 'Variable name: ', Variable.attrib['name']
        elif Variable.find('./Index').text != str(i):
            Variable.clear()                         #insted of ObjectDictionary.remove(Variable)

    for Record in ObjectDictionary.iter('Record'):             #searching for Records in index
        if Record.find('./Index').text == str(i):
            print 'Record name: ', Record.attrib['name']
        elif Record.find('./Index').text != str(i):
            Record.clear()                             #insted of ObjectDictionary.remove(Record)

    for Array in ObjectDictionary.iter('Array'):               #searching for Arrays in index
        if Array.find('./Index').text == str(i):
            print 'Array name: ', Array.attrib['name']
        elif Array.find('./Index').text != str(i):
            Array.clear()                               #insted of ObjectDictionary.remove(Array)


tree.write('output.xml')            #create a new xml file

现在在新的xml文件中仅保留变量,记录和数组的标签.

In new xml file is now left just tags of variables,records and arrays.

<Group name="OptionalObjects"> <Array/><Array/><Variable/><Variable/><Variable/><Record/><Record/><Record/><Record/><Record/><Record/><Record/><Record/></Group>

<Group name="OptionalObjects"> <Array/><Array/><Variable/><Variable/><Variable/><Record/><Record/><Record/><Record/><Record/><Record/><Record/><Record/></Group>

有人知道如何解决这个(我希望)最后一个问题吗?

Have anybody any idea how to solved out this (I hope) last problem?

这篇关于Python:修改XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆