用XML声明python编码 [英] Encoding in XML declaration python

查看:70
本文介绍了用XML声明python编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用python创建了XML文件。但是XML声明只有版本信息。如何在XML声明中包含编码,例如:

I have created an XML file using python. But the XML declaration has only version info. How can I include encoding with XML declaration like:

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


推荐答案

>>> 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()函数设置编码。

you can set the encoding for the document.writexml() function in the same way.

这篇关于用XML声明python编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆