Python中的XML声明编码
我用Python创建了一个XML文件。但是这个XML声明里只有版本信息。我想知道怎么在XML声明中加上编码信息,比如:
<?xml version="1.0" encoding="UTF-8"?>
1 个回答
12
>>> from xml.dom.minidom import Document
>>> a=Document()
>>> a.toprettyxml(encoding="utf-8")
'<?xml version="1.0" encoding="utf-8"?>\n'
或者
>>> a.toxml(encoding="utf-8")
'<?xml version="1.0" encoding="utf-8"?>'
你也可以用同样的方法设置 document.writexml()
函数的编码。