XML声明python中的编码

2024-05-18 07:34:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python创建了一个XML文件。但是XML声明只有版本信息。如何使用XML声明包括编码,如:

<?xml version="1.0" encoding="UTF-8"?> 

Tags: 文件声明编码versionxmlutfencoding版本信息
1条回答
网友
1楼 · 发布于 2024-05-18 07:34:55
>>> 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()函数的编码。

相关问题 更多 >