简体   繁体   English

解析修改多个XML文件

[英]Parse and modify multiple XML files

I am trying to我在尝试着

  1. read multiple XML files from a folder从文件夹中读取多个 XML 文件
  2. modify them (remove the content of two tags)修改它们(删除两个标签的内容)
  3. replace the old XML file with the new one用新文件替换旧的 XML 文件

This is the code I got so far:这是我到目前为止得到的代码:

import xml.etree.ElementTree as ET
import glob

#parse xml files
filenames = glob.glob("[0-9][a-z](*).xml")

for filename in filenames:

    with open(filename, 'r', encoding="utf-8") as content:

        tree = ET.parse(content)

        lst_jugador = tree.findall('data/test')

        for jugador in lst_jugador:

             print (jugador.find('name').text, jugador.get("id"))


myroot = tree.getroot()

# remove tag content
for x in filenames:
     myroot = tree.getroot()
for x in myroot[1][2]:
    x.text = None
for x in myroot[1][17]:
    x.text = None
    filenames.write('data/new.xml')

I get a NameError: name 'tree' is not defined .我收到NameError: name 'tree' is not defined

Any ideas on what I am doing wrong or how to do this in a different way?关于我做错了什么或如何以不同的方式做到这一点的任何想法? I was able to do this with a single xml file, I am struggling with reading and writing multiple xml files.我能够用一个 xml 文件做到这一点,我正在努力读写多个 xml 文件。

This is how the code looked like for reading and writing a single file (which works):这是读取和写入单个文件的代码(有效):

import xml.etree.ElementTree as ET

mytree = ET.parse('data/test.xml')
myroot = mytree.getroot()

for x in myroot[1][2]:
    x.text = None
for x in myroot[1][17]:
    x.text = None
    mytree.write('data/new.xml')

This is a shortened version of my xml file:这是我的 xml 文件的简化版本:

    <xml_file>
    <tag>
        <tag_1>00000000</tag_1>
    </tag>
    <test>
        <one>
            <one_1>test</one_1>
            <one_2>test</one_2>
            <one_3>test</one_3>
            <one_4>test</one_4>
            <one_5>test</one_5>
            <one_6>test</one_6>
            <one_7>test</one_7>
            <one_8>test</one_8>
            <one_9>test</one_9>
            <one_10>test</one_10>
            <one_11>test</one_11>
            <one_12>test</one_12>
        </one>
        <two>
            <two_1>test</two_1>
            <two_2>test</two_2>
            <two_3>test</two_3>
            <two_4>test</two_4>
            <two_5>test</two_5>
            <two_6>test</two_6>
            <two_7>test</two_7>
            <two_8>test</two_8>
        </two>
    </test>
</xml_file>

Simply generalize your working process in a defined method then iteratively pass files into function using map .只需在定义的方法中概括您的工作流程,然后使用map迭代地将文件传递到 function 中。

import xml.etree.ElementTree as ET
import glob

# DEFINED METHOD RECEIVING FILE AS PARAMETER
def update_xml(xml_file)
    mytree = ET.parse(xml_file)
    myroot = mytree.getroot()

    for x in myroot[1][2]:
        x.text = None
    for x in myroot[1][17]:
        x.text = None

    mytree.write(xml_file)      # WRITE NEW TREE TO DISK
    return mytree               # RETURN NEW TREE IN FUNCTION


# ITERATIVELY UPDATE XML WITH map()
filenames = glob.glob("[0-9][a-z](*).xml")

new_trees = list(map(update_xml, filenames))

This is a formatting issue, you need to have 'tree = ET.parse(content)' on the same level as 'myroot = tree.getroot()'这是一个格式问题,您需要将“tree = ET.parse(content)”与“myroot = tree.getroot()”放在同一级别

try this试试这个

import xml.etree.ElementTree as ET
import glob

#parse xml files
filenames = glob.glob("[0-9][a-z](*).xml")

for filename in filenames:

    with open(filename, 'r', encoding="utf-8") as content:

        tree = ET.parse(content)

        lst_jugador = tree.findall('data/test')

        for jugador in lst_jugador:

             print (jugador.find('name').text, jugador.get("id"))

tree = ET.parse(content)
myroot = tree.getroot()

# remove tag content
for x in filenames:
     myroot = tree.getroot()
for x in myroot[1][2]:
    x.text = None
for x in myroot[1][17]:
    x.text = None
    filenames.write('data/new.xml')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM