示例XML文件
<ArticleSet>
<Article>
<ForeName>a</ForeName>
<LastName>b</LastName>
<Affiliation>harvard university of science. abc@gmail.com</Affiliation>
<Keywords>-</Keywords>
</Article>
<Article>
<ForeName>a</ForeName>
<LastName>b</LastName>
<Affiliation>-</Affiliation>
<Keywords>-</Keywords>
</Article>
<Article>
<ForeName>a</ForeName>
<LastName>b</LastName>
<Affiliation>harvard university of science. ghi@yahoo.co.in</Affiliation>
<Keywords>-</Keywords>
</Article>
</ArticleSet>
示例代码
^{pr2}$输出所需更新的XML文件
<?xml version="1.0"?>
<ArticleSet>
<Article>
<ForeName>a</ForeName>
<LastName>b</LastName>
<Affiliation>harvard university of science. abc@gmail.com</Affiliation>
<Keywords>-</Keywords>
<Email>abc@gmail.com</Email>
</Article>
<Article>
<ForeName>a</ForeName>
<LastName>b</LastName>
<Affiliation>-</Affiliation>
<Keywords>-</Keywords>
<Email>-</Email>
</Article>
<Article>
<ForeName>a</ForeName>
<LastName>b</LastName>
<Affiliation>harvard university of science. ghi@yahoo.co.in</Affiliation>
<Keywords>-</Keywords>
<Email>ghi@yahoo.co.in</Email>
</Article>
</ArticleSet>
我想从<Affiliation>
标记中提取电子邮件地址,并创建一个名为<Email>
的新标记并将提取的电子邮件存储到该标记中。如果<Affiliation>
等于-
,则将<Email>-</Email>
存储到该文章中。在
错误
Traceback (most recent call last): File "C:/Users/Ghost Rider/Documents/Python/addingTagsToXML.py", line 11, in etree.write(article,c) AttributeError: module 'xml.etree.ElementTree' has no attribute 'write'
你可以试试这个:
可以使用lxml实例xml图书馆。这个代码运行良好
如果要使用
write
,应按如下方式更正etree导入:而且您不应该使用
etree
作为ElementTree的别名,因为它会使etree
python内置模块过度疲劳!在此外,我认为您误解了
write
函数的含义,因为它只能将结果树写入文件。如果要修改elemtree,应该在元素上使用append
、extend
等。在相关问题 更多 >
编程相关推荐