Come prevenire lxml prom elementi compattazione?
-
05-10-2019 - |
Domanda
Avere seguente codice Python:
>>> from lxml import etree
>>> root = etree.XML("<a><b></b></a>")
>>> etree.tostring(root)
'<a><b/></a>'
Come posso forzare lxml per l'uso "lungo" versione?
come
>>> etree.tostring(root)
'<a><b></b></a>'
Soluzione
>>> import lxml.html
>>> html = lxml.html.fromstring('<a><b></b></a>')
>>> lxml.html.tostring(html)
'<a><b></b></a>'
miscelazione opere così:
>>> from lxml import etree
>>> import lxml.html
>>> xml = etree.XML('<a><b/></a>')
>>> lxml.html.tostring(xml)
'<a><b></b></a>'
Altri suggerimenti
Perché vuoi? Entrambi sono equivalenti in termini di modello di dati di XML.
dalla documentazione
http://lxml.de/tutorial.html#serialisation
from lxml import etree
xml = etree.XML('<a><b/></a>')
etree.tostring(xml, method='html')
'<a><b></b></a>'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow