_csv.Error: string with NUL bytes

fscked fsckedagain at gmail.com
Thu May 3 12:18:10 EDT 2007


On May 3, 9:11 am, Larry Bates <larry.ba... at websafe.com> wrote:
> fscked wrote:
> > Anyone have an idea of what I might do to fix this? I have googled adn
> > can only find some random conversations about it that doesn't make
> > sense to me.
>
> > I am basically reading in a csv file to create an xml and get this
> > error.
>
> > I don't see any empty values in any fields or anything...
>
> 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




More information about the Python-list mailing list