python - 替换 XML 文件中的单词

标签 python regex xml replace xml-parsing

我需要编写 python 脚本来替换 xml 文件中所有出现的特定单词。我只需要替换标记中包含的禁用词。

这应该被替换:

<some_xml_tag>some text REPLACE_ME some text</some_xml_tag>

这不应该:

<some_xml_tag attr="REPLACE_ME">some text</some_xml_tag>

<REPLACE_ME>some text</REPLACE_ME>

我不是正则表达式专家,但它是否可行?

最佳答案

改用XML 解析器

示例使用 lxml图书馆。这里我们使用 xpath() 搜索具有所需文本的节点,然后使用 replace() 替换它:

import lxml.etree as ET

ban_word = 'REPLACE_ME'
replacement = 'HELLO'

data = """<root>
    <some_xml_tag>REPLACE_ME</some_xml_tag>
    <some_xml_tag attr="REPLACE_ME">some text</some_xml_tag>
    <REPLACE_ME>some text</REPLACE_ME>
</root>
"""

root = ET.fromstring(data)

for item in root.xpath('//*[. = "%s"]' % ban_word):
    item.text = item.text.replace(ban_word, replacement)

print ET.tostring(root)

打印:

<root>
    <some_xml_tag>HELLO</some_xml_tag>
    <some_xml_tag attr="REPLACE_ME">some text</some_xml_tag>
    <REPLACE_ME>some text</REPLACE_ME>
</root>

注意事项:

  • 比较不区分大小写
  • xml.etree.ElementTree 不会处理这种特殊方法,因为它仅提供有限的 xpath 支持
  • 正如@tdelaney 在评论中指出的那样,如果您有一个要替换的单词列表,那么简单地遍历所有节点并在必要时替换文本可能是个好主意

关于python - 替换 XML 文件中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27869571/

相关文章:

python - 使用 sklearn 获取单词的 tf-idf 权重

python - 验证日期(格式和值)

xml - 同时复制结构和子结构

python - 如何设置导入,以便 Airflow 在任务正常运行时不显示错误

python - 我如何隐藏 turtle 以前画的东西?

Java正则表达式,在标点符号处分割字符串(括号内除外)

regex - Grep/Sed 在两个多行标签之间

html - 使用 SQL FOR XML 创建 HTML 表

java - XPath 歧义

python - python 的多个错误解释器问题(即使使用 virtualenv)