繁体   English   中英

Python在不知道标签的情况下搜索并替换XML文件中的文本(标签的值)

[英]Python search and replace text (value of a tag) in a XML file without knowing the tag

我是Python的新手,正在尝试使用XML文件。 我知道如何解析和搜索了解结构的信息,但是我不知道如何在不知道该值附加到标签的情况下搜索值。

例如 :

<bookstore>
  <book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>TRUE</author>
  <year>2005</year>
  <price>30.00</price>
</book>
  <book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>TRUE</year>
  <price>39.95</price>
  </book>
<adventure>
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>TRUE</year>
  <price>TRUE</price>
</adventure>
</bookstore>

在此示例中,我想找到所有“ TRUE”值并将其替换为“ OK”。 你会怎么做?

谢谢

这是使用标准库中的xml.etree.ElementTree的选项:

import xml.etree.ElementTree as ET

data = """xml here"""

tree = ET.fromstring(data)     
for element in tree.getiterator():
    if element.text == 'TRUE': 
        element.text = 'OK'    

print ET.tostring(tree)   

打印:

<bookstore>
  <book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>OK</author>
  <year>2005</year>
  <price>30.00</price>
</book>
  <book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>OK</year>
  <price>39.95</price>
  </book>
<adventure>
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>OK</year>
  <price>OK</price>
</adventure>
</bookstore>

如果单词TRUE仅在标签之间存在,则应该可以使用简单的字符串替换

my_xml = """
<bookstore>
  <book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>TRUE</author>
  <year>2005</year>
  <price>30.00</price>
</book>
  <book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>TRUE</year>
  <price>39.95</price>
  </book>
</bookstore>
"""
>>> my_xml.replace(">TRUE<",">OK<")
'\n<bookstore>\n  <book category="COOKING">\n  <title lang="en">Everyday Italian</title>\n  <author>OK</author>\n  <year>2005</year>\n  <price>30.00</price>\n</book>\n  <book category="CHILDREN">\n  <title lang="en">Harry Potter</title>\n  <author>J K. Rowling</author>\n  <year>2005</year>\n  <price>29.99</price>\n</book>\n<book category="WEB">\n  <title lang="en">Learning XML</title>\n  <author>Erik T. Ray</author>\n  <year>OK</year>\n  <price>39.95</price>\n  </book>\n</bookstore>\n'
>>> 

绝对不如使用xml lib健壮,但应该可以完成工作。

在这里,我做了什么,并允许我在xml文件中找到所有值。

for node in root.iter():
        if (node.text != None):
            node.text = search_in_dictonary_foot(">"+node.text+"<")

暂无
暂无

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

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