使用 Python,我试图弄清楚如何根据来自 CSV 的输入创建 XML 文件。在从 CSV 中获取一行数据之前,必须使用相同的标签添加一段静态信息。这可以用于 1 行数据或 20 行。DocFile 标签的数量应该是行数的两倍。
<DocFile>
<FullPathName>P:\StaticFile.pdf</FullPathName>
<PageRange UseAllPages="true"/>
<Quantity>1</Quantity>
<Section>1</Section>
</DocFile>
<DocFile>
<FullPathName>row[0]</FullPathName>
<PageRange UseAllPages="true"/>
<Quantity>row[1]</Quantity>
<Section>1</Section>
</DocFile>
这是 1 行数据的示例。
以下是我到目前为止所写的内容。我遇到的问题是只有一部分 DocFile 将其转换为 XML。仅供参考,我对 Python 比较陌生。
`import csv
from lxml import etree as ET
# Assign file locations
csvFile = '/Users/jehringer/python_work/Work Testing/CSV/Order_123.csv'
xmlFile = '/Users/jehringer/python_work/Work Testing/CSV/myXMLFile.xml'
# Build XML Structure
root = ET.Element('UltimateImposition')
redirect = ET.SubElement(root, 'Redirection')
printJob = ET.SubElement(redirect, 'PrintJob')
queue = ET.SubElement(printJob, 'QueueName')
documents = ET.SubElement(printJob, "Documents")
docFile = ET.SubElement(documents, "DocFile")
fullName = ET.SubElement(docFile, "FullPathName")
pageCount = ET.SubElement(docFile, "PageRange")
qty = ET.SubElement(docFile, "Quantity")
section = ET.SubElement(docFile, "Section")
# Define Static Values
ET.SubElement(queue, "Name").text = "process"
ET.SubElement(queue, "FullPathName").text = r"P:\process"
# Open CSV and run through
with open(csvFile, 'r') as csvFiles:
csvData = csv.reader(csvFiles, delimiter=',')
for row in csvData:
for i in range(2):
if i == 1:
fullName.text = "2up_v4_ejectjob.pdf"
pageCount.set("UseAllPages", "true")
qty.text = "1"
section.text = '1'
else:
fullName.text = row[0]
pageCount.set("UseAllPages", "true")
qty.text = row[1]
section.text = '1'
# Write to XML
tree = ET.ElementTree(root)
tree.write(xmlFile, pretty_print=True, xml_declaration=True)`
慕婉清6462132
烙印99
相关分类