将 CDATA 添加到 XML 个字段
Adding CDATA to XML fields
如何将 CDATA 添加到 python 从 xlsx 到 xml 中的所有生成字段?
代码如下:
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")
在输出中我需要:
<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>
答案是加上CDATA
,例如:
Column_heading_1.text = et.CDATA(str(row[1]['sku']))