PYTHON 2.6 XML.ETREE to output single quote for attributes instead of double quote

不想你离开。 提交于 2019-12-04 15:39:55

I checked the documentation and found no reference for single/double-quote option.

I think your only recourse is print etree.tostring(n).replace('"', "'")

Update

Given:

from lxml import etree
n = etree.Element('test')
n.set('id', "Zach's not-so-good answer")

my original answer could output malformed XML because of unbalanced apostrophes:

<test id='Zach's not-so-good answer'></test>

Martijn suggested print etree.tostring(n).replace("'", '&apos;').replace('"', "'") to address the problem:

<test id='Zach&apos;s not-so-good answer'></test>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!