You really should post some code and the actual traceback error your
get for us to help. I suspect that you have an ill-formed record in
your CSV file. If you can't control that, you may have to write your
own CSV dialect parser.
-Larry
Certainly, here is the code:
import os,sys
import csv
from elementtree.ElementTree import Element, SubElement, ElementTree
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
root = Element("{Boxes}boxes")
myfile = open('test.csv', 'rb')
csvreader = csv.reader(myfile)
for boxid, mac, activated, hw_ver, sw_ver, heartbeat, name, address,
phone, country, city, in csvreader:
mainbox = SubElement(root, "{Boxes}box")
mainbox.attrib["city"] = city
mainbox.attrib["country"] = country
mainbox.attrib["phone"] = phone
mainbox.attrib["address"] = address
mainbox.attrib["name"] = name
mainbox.attrib["pl_heartbeat"] = heartbeat
mainbox.attrib["sw_ver"] = sw_ver
mainbox.attrib["hw_ver"] = hw_ver
mainbox.attrib["date_activated"] = activated
mainbox.attrib["mac_address"] = mac
mainbox.attrib["boxid"] = boxid
indent(root)
ElementTree(root).write('test.xml', encoding='UTF-8')
The traceback is as follows:
Traceback (most recent call last):
File "createXMLPackage.py", line 35, in ?
for boxid, mac, activated, hw_ver, sw_ver, heartbeat, name,
address, phone, country, city, in csvreader:
_csv.Error: string with NUL bytes
Exit code: 1 , 0001h