gpt4 book ai didi

Python - XML - LXML - 如何使用示例 XML 文件中的相同 XML 树结构来创建另一个 XML 文件

转载 作者:行者123 更新时间:2023-12-01 08:05:55 26 4
gpt4 key购买 nike

我有一个 XML 文件,其中包含一些树结构、元素、属性、文本。我需要使用此 XML 作为模板(树结构和标签)并创建另一个 XML,它可能具有不同数量的元素(即,在下面的模板中有两个“列”元素,但我要创建的元素有三个元素“列”)。

下面是我想用作模板的 XML

<custom-data>
<schema>SCHEMA</schema>
<columns>
<column>
<name>ORGANIZATION</name>
<datatype>NUMBER</datatype>
<length/>
<precision>18</precision>
<not-null>Yes</not-null>
</column>
<column>
<name>LOCATION</name>
<datatype>NUMBER</datatype>
<length/>
<precision>18</precision>
<not-null>Yes</not-null>
</column>
</columns>
</custom-data>

不要使用 lxml 定义类似的树,而是通过一一定义每个元素,如下所示。例如,如果“df”是我的带有数据的 pandas 数据框。其中列为(目标列、数据类型、长度、比例、可为空。_

Target Column   Data Type   Length  Scale   Nullable
COLUMN1 NUMBER 38 0 N
COLUMN2 NUMBER 38 0 N
COLUMN3 NUMBER 38 0 N

下面是我正在使用的示例 python 代码

from lxml import etree as et
root = et.Element('custom-data')
schema= et.SubElement(root, 'schema')
schema.text='SCHEMA'
columns= et.SubElement(root, 'columns')

for row in df.iterrows():
column = et.SubElement(columns, 'columns')
name = et.SubElement(column , 'name')
datatype = et.SubElement(column , 'datatype')
length = et.SubElement(column , 'length')
precision = et.SubElement(column , 'precision')
notnull = et.SubElement(column , 'not-null')

name.text = str(row[1]['Target Column'])
datatype.text = str(row[1]['Data Type'])
length.text = str(row[1]['Length'])
precision.text = str(row[1]['Scale'])
notnull.text = str(row[1]['Nullable'])

xml_test=et.tostring(root, pretty_print=True).decode('utf-8')
f=open("xml_test.xml", "w")
f.write(xml_test)

预期输出为

<custom-data>
<schema>SCHEMA</schema>
<columns>
<column>
<name>COLUMN1</name>
<datatype>NUMBER</datatype>
<length>38</length>
<precision>0</precision>
<not-null>N</not-null>
</column>
<column>
<name>COLUMN2</name>
<datatype>NUMBER</datatype>
<length>38</length>
<precision>0</precision>
<not-null>N</not-null>
</column>
<column>
<name>COLUMN3</name>
<datatype>NUMBER</datatype>
<length>38</length>
<precision>0</precision>
<not-null>N</not-null>
</column>
</columns>
</custom-data>

如何利用示例 XML 中给出的结构,以便不需要在代码中再次定义它。有没有更简单的方法?

最佳答案

您应该能够解析 XML 模板并使用“column”元素的副本来制作用 DataFrame 中的数据填充的新副本。

应该清理和简化模板,使其仅包含静态值和用作模板的单个“列”元素。

这是一个例子...

XML 模板 (template.xml)

<custom-data>
<schema>SCHEMA</schema>
<columns>
<column>
<name/>
<datatype/>
<length/>
<precision/>
<not-null/>
</column>
</columns>
</custom-data>

Python

import pandas as pd
from copy import deepcopy
from lxml import etree as et

# Mock up DataFrame for testing.
data = {"Target Column": ["COLUMN1", "COLUMN2", "COLUMN3"],
"Data Type": ["NUMBER", "NUMBER", "NUMBER"],
"Length": [38, 38, 38],
"Scale": [0, 0, 0],
"Nullable": ["N", "N", "N"]}
df = pd.DataFrame(data=data)

# Create ElementTree from template XML.
tree = et.parse("template.xml")

# Map column names to element names.
name_map = {"Target Column": "name",
"Data Type": "datatype",
"Length": "length",
"Scale": "precision",
"Nullable": "not-null"}

# Select "columns" element so we can append/remove children.
columns_elem = tree.xpath("/custom-data/columns")[0]
# Select "column" element to use as a template for new column elements.
column_template = columns_elem.xpath("column")[0]

for row in df.iterrows():
# Create a new copy of the column template.
new_column = deepcopy(column_template)

# Populate elements in column template based on name map.
for col_name, elem_name in name_map.items():
new_column.xpath(elem_name)[0].text = str(row[1][col_name])

# Append the new column element to "columns" element.
columns_elem.append(new_column)

# Remove original empty column template.
columns_elem.remove(column_template)

# Write tree to file.
# Note: I could've just done tree.write("output.xml"), but I used
# XMLParser(), tostring(), and fromstring() to get the indents
# all the same.
parser = et.XMLParser(remove_blank_text=True)
et.ElementTree(et.fromstring(et.tostring(tree),
parser=parser)).write("output.xml",
pretty_print=True)

XML 输出(“output.xml”)

<custom-data>
<schema>SCHEMA</schema>
<columns>
<column>
<name>COLUMN1</name>
<datatype>NUMBER</datatype>
<length>38</length>
<precision>0</precision>
<not-null>N</not-null>
</column>
<column>
<name>COLUMN2</name>
<datatype>NUMBER</datatype>
<length>38</length>
<precision>0</precision>
<not-null>N</not-null>
</column>
<column>
<name>COLUMN3</name>
<datatype>NUMBER</datatype>
<length>38</length>
<precision>0</precision>
<not-null>N</not-null>
</column>
</columns>
</custom-data>

关于Python - XML - LXML - 如何使用示例 XML 文件中的相同 XML 树结构来创建另一个 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55534115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com