数据.xml
\n\n<?xml version="1.0" encoding="UTF-8"?>\n<ArticleSet>\n <Article> \n <LastName>Bojarski</LastName>\n <ForeName>-</ForeName>\n <Affiliation>-</Affiliation> \n </Article>\n <Article> \n <LastName>Gen\xc3\xa7</LastName>\n <ForeName>Yasemin</ForeName>\n <Affiliation>fgjfgnfgn</Affiliation> \n </Article>\n</ArticleSet>\nRun Code Online (Sandbox Code Playgroud)\n\n示例代码
\n\nfrom lxml import etree\n\ndom = etree.parse(\'data.xml\')\nroot = dom.getroot()\n\nfor article in dom.xpath(\'Article[Affiliation="-"]\'):\n root.remove(article)\n\ndom.write(\'output.xml\')\nRun Code Online (Sandbox Code Playgroud)\n\n此代码删除其隶属关系等于的文章 - 即其隶属标签看起来像<Affliation>-</Affliation>\n当我将剩余的输出存储到 output.xml 中时,它会将 Unicode 字符解析Gen\xc3\xa7为Genç我想按原样存储它。
代码的输出
\n\n<ArticleSet>\n <Article> \n <LastName>Genç</LastName>\n <ForeName>Yasemin</ForeName>\n <Affiliation>fgjfgnfgn</Affiliation> \n </Article>\n</ArticleSet>\nRun Code Online (Sandbox Code Playgroud)\n\n所需输出
\n\n<ArticleSet>\n <Article> \n <LastName>Gen\xc3\xa7</LastName>\n <ForeName>Yasemin</ForeName>\n <Affiliation>fgjfgnfgn</Affiliation> \n </Article>\n</ArticleSet>\nRun Code Online (Sandbox Code Playgroud)\n