How to add CDATA to all generated fields in python from xlsx to xml?
Code looks like:
from lxml import etree as et
raw_data = pd.read_excel(r'path_to_file')
root = et.Element('document')
for row in raw_data.iterrows():
root_tags = et.SubElement(root, 'root')
# These are the tag names for each row
Column_heading_1 = et.SubElement(root_tags, 'sku')
Column_heading_2 = et.SubElement(root_tags, 'product_url')
# The values inside the [] are the raw file column headings.
Column_heading_1.text = str(row[1]['sku'])
Column_heading_2.text = str(row[1]['product_url'])
tree = et.ElementTree(root)
et.indent(tree, space="\t", level=0)
tree.write('output.xml', encoding="utf-8")
and in output I need:
<document>
<root>
<sku><![CDATA[Z.18181.16158141231205807]]></sku>
<product_url><![CDATA[https://link.test]]></product_url>
</root>
<root>
<sku><![CDATA[Z.18181.16158141231205807]]></sku>
<product_url><![CDATA[https://link.test]]></product_url>
</root>
</document>
The answer is to add CDATA
, eg:
Column_heading_1.text = et.CDATA(str(row[1]['sku']))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.