如何解决如何在Python / ElementTree
我正在尝试为XML中的单词参考源文件创建XML文件。当我写入文件时,仅显示“ xml_decaration = True”,它显示<?xml version='1.0' encoding='us-ascii'?>
,但我希望它以<?xml version="1.0"?>
的形式出现。
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
import xml.etree.ElementTree as ET
import uuid
from lxml import etree
root=Element('b:sources')
root.set('SelectedStyle','')
root.set('xmlns:b','http://schemas.openxmlformats.org/officeDocument/2006/bibliography')
root.set('xmlns','http://schemas.openxmlformats.org/officeDocument/2006/bibliography')
#root.attrib=('SelectedStyle'='','xmlns:b'='"http://schemas.openxmlformats.org/officeDocument/2006/bibliography"','xmlns'='"http://schemas.openxmlformats.org/officeDocument/2006/bibliography"')
source=ET.SubElement(root,'b:source')
ET.SubElement(source,'b:Tag')
ET.SubElement(source,'b:SourceType').text='Misc'
ET.SubElement(source,'b:guid').text=str(uuid.uuid1())
Author=ET.SubElement(source,'b:Author')
Author2=ET.SubElement(Author,'b:Author')
ET.SubElement(Author2,'b:Corporate').text='Norsk olje og gass'
ET.SubElement(source,'b:Title').text='R-002'
ET.SubElement(source,'b:Year').text='2019'
ET.SubElement(source,'b:Month').text='10'
ET.SubElement(source,'b:Day').text='27'
tree=ElementTree(root)
tree.write('Sources.xml',xml_declaration=True,method='xml')
解决方法
ElementTree.write()
具有以下签名:
write(file,encoding="us-ascii",xml_declaration=None,default_namespace=None,method="xml",*,short_empty_elements=True)
如果要XML的默认编码为encoding=""
,请致电UTF-8
。
相关:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。