如何解决编写用下划线替换连字符的编辑过的 xml
因此,我正在尝试编写一个新的 xml 文件,该文件是我从原始文件中编辑的,方法是将连字符替换为下划线,然后开始处理该 xml 文件的其余代码。
这是我的代码:
import xml.etree.ElementTree as ET
from lxml import etree
#attaching xml file
xmlfile = "hook_zap.xml"
tree = ET.parse(xmlfile)
root = tree.getroot()
#replace hypen with underscore within the xml
doc = etree.parse(xmlfile)
for e in doc.xpath('//*[contains(local-name(),"-")]'):
e.tag = e.tag.replace('-','_')
refracted = etree.tostring(doc,method='xml')
#create a new xml file with refracted file
refracted.write('base.xml')
#print (refracted)
而且我不断收到此错误:
AttributeError: 'bytes' 对象没有属性 'write'
解决方法
将 refracted
像任何其他类型的数据一样写入文件:
with open('base.xml','w') as f:
f.write(refracted.decode('utf-8'))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。