【问题标题】:How to search and replace text in an XML file using Python?如何使用 Python 搜索和替换 XML 文件中的文本?
【发布时间】:2016-10-18 13:01:41
【问题描述】:

如何在整个 xml 文件中搜索特定的文本模式,然后在 Python 3.5 中用新的文本模式替换该文本的每次出现?

其他所有内容(格式、属性、cmets 等)都需要在原始 xml 文件中保持原样。

我在 Windows (win32) 上运行 Python 3.5.1。

具体来说,我想将每次出现的“FEATURE NAME”替换为“THIS WORKED”,并将每次出现的“FEATURE NUMBER”替换为“12345”。

我一直在尝试学习 Python 和 xml.etree.ElementTree,但无法弄清楚这一点。我已经看过“在 Python 中搜索和替换 .xml 文件中的一行”、“在 Python 中搜索和替换文件中的一行”和“如何使用 Python 搜索和替换文件中的文本?”和本网站上的其他现有 Q/A,但无法弄清楚 - 我不是经验丰富的程序员,所以如果需要更多输入,请告诉我。非常感谢您的帮助!!!

这是我在记事本中打开 xml 代码时的副本(除了我添加了空格以缩进每行并在将其粘贴到此问题时为某些行按回车键):

<description-topic>
    <access-info>
        <index-term-set>
            <index-term>
                <primary>FID FEATURE NUMBER</primary>
            </index-term>
            <index-term>
                <primary>FEATURE NAME</primary>
            </index-term>
            <index-term>
                <primary>Common features</primary>
                <secondary>FID FEATURE NUMBER</secondary>
            </index-term>
        </index-term-set>
    </access-info>
    <title>FEATURE NUMBER - FEATURE NAME</title>
    <block>
        <label>Platform</label>
        <comment>REVIEWERS: I guessed at the FEATURE NAME</comment>
        <para>
            This feature applies to the following platforms: FEATURE NAME<!--Check the values--></para>
    </block>
    <block branch="no">
        <label>Feature Benefits</label>
        <para>
            <comment>REVIEWERS: What do we put here? See template (link given in review email) for more information.</comment>
        </para>
    </block>
    <block branch="no">
        <label>Dependencies</label>
        <para/>
        <subblock>
            <label>Features</label>
            <comment>What FEATURE NAME do we put here?</comment>
        </subblock>
        <subblock>
            <label>Hardware</label>
            <comment>What FEATURE NAME do we put here?</comment>
            <para>This feature applies to the following: FEATURE NUMBER and text.</para><?Pub Caret -1?>
        </subblock>
        <subblock>
            <label>Dependencies outside the eNodeB</label>
            <comment>What FEATURE NAME do we put here?</comment>
        </subblock>
    </block>
    <block branch="no">
        <label>Impacts</label>
        <comment>REVIEWERS: What FEATURE NUMBER do we put here?</comment>
        <para>
            <comment/>
        </para>
    </block>
</description-topic>

这是我要开始工作的最新代码:

from xml.etree import ElementTree as et
tree = et.parse('Atemplate2.xml')
tree.find('description-topic/access-info/index-term-set/index-term/primary/').text = '12345'
tree.write('Atemplate2.xml')

我收到以下错误: 回溯(最近一次通话最后): 文件“ajktest18.py”,第 15 行,在 tree.find('description-topic/access-info/index-term-set/index-term/primary/').text = '12345'

AttributeError: 'NoneType' 对象没有属性 'text'

我希望能够搜索和修改整个文件中的任何匹配项,但我不知道如何找到我正在搜索的文本的一个特定匹配项。

这是我试图用来查找路径的代码:

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

print(root.tag, root.attrib, root.text)

for child in root:
    print(child.tag, child.attrib, child.text)
for label in root.iter('label'):
    print(label.tag, label.attrib, label.text)
for title in root.iter('title'):
    print(title.attrib)

我也试过下面的代码:

with open('Atemplate2.xml') as f:
    tree = ET.parse(f)
    root = tree.getroot()

for elem in root.getiterator():
    try:
        elem.text = elem.text.replace('FEATURE NAME', 'THIS WORKED')
        elem.text = elem.text.replace('FEATURE NUMBER', '12345')
    except AttributeError:
        pass

tree.write('output.xml')

但这给出了以下错误:

File "<pyshell#40>", line 2, in <module>
    tree = ET.parse(f)
File "C:\MyPath\Python35-32\lib\xml\etree\ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
File "C:\ MyPath \Python35-32\lib\xml\etree\ElementTree.py", line 594, in parse
    self._root = parser._parse_whole(source)
File "C:\ MyPath \Python35-32\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]

UnicodeDecodeError:“charmap”编解码器无法解码位置 1119 中的字节 0x9d:字符映射到

# #

最终更新 - 这是最后对我有用的代码(谢谢你,Jarad!):

import lxml.etree as ET
#using lxml instead of xml preserved the comments

#adding the encoding when the file is opened and written is needed to avoid a charmap error
with open('filename.xml', encoding="utf8") as f:
  tree = ET.parse(f)
  root = tree.getroot()


  for elem in root.getiterator():
    try:
      elem.text = elem.text.replace('FEATURE NAME', 'THIS WORKED')
      elem.text = elem.text.replace('FEATURE NUMBER', '123456')
    except AttributeError:
      pass

#tree.write('output.xml', encoding="utf8")
# Adding the xml_declaration and method helped keep the header info at the top of the file.
tree.write('output.xml', xml_declaration=True, method='xml', encoding="utf8")

【问题讨论】:

  • 您的示例 xml 不包含 abc
  • 我正在尝试学习如何做到这一点并使某些事情自动化。这就是我在这里的原因。我有 Python 培训书籍并创建了程序并对其进行了修改。我不知道如何做这部分。我的评论是我可能遗漏了一些非常简单和明显的东西......或者只是让我知道是否需要额外的输入来帮助回答我的问题。感谢任何可以提供帮助的人。
  • 很公平,那么您应该发布您编写的代码,并检查how to ask
  • @spectras - 我发布了更多信息,包括代码并删除了通用 abc 文本参考,但保留了更具体的文本。谢谢

标签: python xml search replace


【解决方案1】:

注意事项:

  • 我从未使用过 xml.etree.ElementTree
  • 我从未使用过它,因为我从未发现自己在操作 XML
  • 我不知道与知道进出图书馆的人相比,这是否是“最佳”方式
  • 评论者似乎一心要评判你,而不是帮助你

这是对this excellent answer 的修改。问题是,您需要读取 XML 文件并对其进行解析。

import xml.etree.ElementTree as ET

with open('xmlfile.xml', encoding='latin-1') as f:
  tree = ET.parse(f)
  root = tree.getroot()

  for elem in root.getiterator():
    try:
      elem.text = elem.text.replace('FEATURE NAME', 'THIS WORKED')
      elem.text = elem.text.replace('FEATURE NUMBER', '123456')
    except AttributeError:
      pass

tree.write('output.xml', encoding='latin-1')

请注意,您可以将encoding 参数更改为其他内容,例如:utf-8cp1252ISO-8859-1 等。这取决于您的系统和文件。

【讨论】:

  • 谢谢。您提供的“优秀答案”帖子看起来就像我正在尝试做的一样。
  • 我尝试了您提供的代码并收到以下错误: Traceback (last recent call last): File "", line 2, in tree = ET.parse( f) 文件“C:\MyPath\Python35-32\lib\xml\etree\ElementTree.py”,第 1182 行,在 parse tree.parse(source, parser) 文件“C:\MyPath\Python35-32\lib\ xml\etree\ElementTree.py",第 594 行,解析 self._root = parser._parse_whole(source)
  • 不看你的代码很难说。你能在你的帖子后面加上一个显示你的新方法的编辑吗?
  • 我编辑了我的帖子,并为您提供的示例包含了我的代码。感谢您的帮助。
  • 在我看来这可能是一个编码问题。将with open('xmlfile.xml') as f: 更改为具有类似with open('xmlfile.xml', encoding='utf-8') as f: 的编码参数。如果utf-8 不起作用,请尝试以下任一方法:cp1252latin-1。此外,当您写入tree.write('output.xml') 时,请务必添加相同的encoding 参数,以便输出文件保留编码。我更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
相关资源
最近更新 更多