【问题标题】:Python search and replace text (value of a tag) in a XML file without knowing the tagPython 在不知道标签的情况下搜索和替换 XML 文件中的文本(标签的值)
【发布时间】:2014-07-21 14:43:05
【问题描述】:

我是 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”。你会怎么做?

谢谢

【问题讨论】:

  • TRUE 是否存在于 XML 文件 标签之外 的任何位置?

标签: python xml parsing search replace


【解决方案1】:

这是一个使用标准库中的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 值都在名为“book”的标签中,但在我的所有 xml 文件中并不总是时间。我编辑了 xml 示例,以便您可以在这里看到我要解释的内容。
  • @user3848394 好吧,首先,它回答了您最初的问题。我已经更新了答案,因此它适用于那里的任何节点。希望对您有所帮助。
【解决方案2】:

如果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 库那么健壮,但应该可以完成工作。

【讨论】:

  • 是的,这是一个解决方案,但我想使用一些 python 的 lib 函数。拥有更强大的东西......但这是个好主意,如果我找不到其他方法,我会这样做! Thnak的
【解决方案3】:

这是我所做的并允许我在我的 xml 文件中找到所有值。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多