0

Using Python, I am trying to figure out how to create a XML file based on the input from a CSV. Before a row of data is brought over from the CSV, a section of static information has to be added using the same tags. This could be for 1 row of data or 20 rows. The # of DocFile tags should be double the amount of rows.

    <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>

This is a sample of 1 row of data.

Below is what I have written so far. The problem that I am having is that only one section of DocFile is making it to the XML. FYI I am relatively new to 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)`
JesseE
  • 33
  • 8
  • Possible duplicate of [How do convert a pandas/dataframe to XML?](https://stackoverflow.com/questions/18574108/how-do-convert-a-pandas-dataframe-to-xml) – rpanai Feb 23 '19 at 02:11
  • What is the problem? What code have you written so far? – mzjn Feb 25 '19 at 06:44
  • I just added the code to the question. Right now I can only get 1 block of DocFile to write over to the XML. – JesseE Feb 25 '19 at 14:32

2 Answers2

0

There no CSV standard, it is just a way to represent a spreadsheet using common text so it really depends on how your CSV is formatted.

You can use the python csv module to read from the csv file. You could then process the data to typically represent them as dict by zipping keys and values. It is easy to fuse or update these data with other data once you have everything as dict, lis or any combination of both types with nesting. Then you could convert the resulting python data types as a xml. I usually rather use json as an exchange format to do this kind of job but this how I would do it.

You should have a look at this : Serialize Python dictionary to XML Or you have also this module which also allow to parse, manipulate and write xml : https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree

michipeka
  • 26
  • 5
0

I figured it out with the help of a friend. We added a function that created the static XML fields that I needed and then called the function everything we looped through the row of the CSV file. Thanks for the input!

JesseE
  • 33
  • 8