473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

_csv.Error: string with NUL bytes

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

May 3 '07 #1
9 11347
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
May 3 '07 #2
On May 3, 9:11 am, Larry Bates <larry.ba...@websafe.comwrote:
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

May 3 '07 #3
In <11**********************@o5g2000hsb.googlegroups. com>, fscked wrote:
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
As Larry said, this most likely means there are null bytes in the CSV file.

Ciao,
Marc 'BlackJack' Rintsch
May 3 '07 #4
On May 3, 9:29 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <1178209090.674787.202...@o5g2000hsb.googlegroups. com>, fscked wrote:
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

As Larry said, this most likely means there are null bytes in the CSV file.

Ciao,
Marc 'BlackJack' Rintsch
How would I go about identifying where it is?

May 3 '07 #5
On Thu, May 03, 2007 at 09:57:38AM -0700, fscked wrote:
As Larry said, this most likely means there are null bytes in the CSV file.

Ciao,
Marc 'BlackJack' Rintsch

How would I go about identifying where it is?
A hex editor might be easiest.

You could also use Python:

print open("filewithnuls").read().replace("\0", ">>>NUL<<<")

Dustin
May 3 '07 #6
On May 3, 10:12 am, dus...@v.igoro.us wrote:
On Thu, May 03, 2007 at 09:57:38AM -0700, fscked wrote:
As Larry said, this most likely means there are null bytes in the CSV file.
Ciao,
Marc 'BlackJack' Rintsch
How would I go about identifying where it is?

A hex editor might be easiest.

You could also use Python:

print open("filewithnuls").read().replace("\0", ">>>NUL<<<")

Dustin
Hmm, interesting if I run:

print open("test.csv").read().replace("\0", ">>>NUL<<<")

every single character gets a >>>NUL<<< between them...

What the heck does that mean?

Example, here is the first field in the csv

89114608511,

the above code produces:
>>>NUL<<<8>>>NUL<<<9>>>NUL<<<1>>>NUL<<<1>>>NUL<<<4 >>>NUL<<<6>>>NUL<<<0>>>NUL<<<8>>>NUL<<<5>>>NUL<<<1 >>>NUL<<<1>>>NUL<<<,
May 3 '07 #7
On Thu, May 03, 2007 at 10:28:34AM -0700, IA********@gmail.com wrote:
On May 3, 10:12 am, dus...@v.igoro.us wrote:
On Thu, May 03, 2007 at 09:57:38AM -0700, fscked wrote:
As Larry said, this most likely means there are null bytes in the CSV file.
Ciao,
Marc 'BlackJack' Rintsch
How would I go about identifying where it is?
A hex editor might be easiest.

You could also use Python:

print open("filewithnuls").read().replace("\0", ">>>NUL<<<")

Dustin

Hmm, interesting if I run:

print open("test.csv").read().replace("\0", ">>>NUL<<<")

every single character gets a >>>NUL<<< between them...

What the heck does that mean?

Example, here is the first field in the csv

89114608511,

the above code produces:
>>NUL<<<8>>>NUL<<<9>>>NUL<<<1>>>NUL<<<1>>>NUL<<<4> >>NUL<<<6>>>NUL<<<0>>>NUL<<<8>>>NUL<<<5>>>NUL<<<1> >>NUL<<<1>>>NUL<<<,
I'm guessing that your file is in UTF-16, then -- Windows seems to do
that a lot. It kind of makes it *not* a CSV file, but oh well. Try

print open("test.csv").decode('utf-16').read().replace("\0", ">>>NUL<<<")

I'm not terribly unicode-savvy, so I'll leave it to others to suggest a
way to get the CSV reader to handle such encoding without reading in the
whole file, decoding it, and setting up a StringIO file.

Dustin
May 3 '07 #8
du****@v.igoro.us wrote:
I'm guessing that your file is in UTF-16, then -- Windows seems to do
that a lot. It kind of makes it *not* a CSV file, but oh well. Try

print open("test.csv").decode('utf-16').read().replace("\0",
">>>NUL<<<")

I'm not terribly unicode-savvy, so I'll leave it to others to suggest a
way to get the CSV reader to handle such encoding without reading in the
whole file, decoding it, and setting up a StringIO file.
Not pretty, but seems to work:

from __future__ import with_statement

import csv
import codecs

def recoding_reader(stream, from_encoding, args=(), kw={}):
intermediate_encoding = "utf8"
efrom = codecs.lookup(from_encoding)
einter = codecs.lookup(intermediate_encoding)
rstream = codecs.StreamRecoder(stream, einter.encode, efrom.decode,
efrom.streamreader, einter.streamwriter)

for row in csv.reader(rstream, *args, **kw):
yield [unicode(column, intermediate_encoding) for column in row]

def main():
file_encoding = "utf16"

# generate sample data:
data = u"\xe4hnlich,\xfcblich\r\nalpha,beta\r\ngamma,delt a\r\n"
with open("tmp.txt", "wb") as f:
f.write(data.encode(file_encoding))

# read it
with open("tmp.txt", "rb") as f:
for row in recoding_reader(f, file_encoding):
print u" | ".join(row)

if __name__ == "__main__":
main()

Data from the file is recoded to UTF-8, then passed to a csv.reader() whose
output is decoded to unicode.

Peter

May 3 '07 #9
On May 4, 3:40 am, dus...@v.igoro.us wrote:
On Thu, May 03, 2007 at 10:28:34AM -0700, IAmStar...@gmail.com wrote:
On May 3, 10:12 am, dus...@v.igoro.us wrote:
On Thu, May 03, 2007 at 09:57:38AM -0700, fscked wrote:
As Larry said, this most likely means there are null bytes in the CSV file.
Ciao,
Marc 'BlackJack' Rintsch
How would I go about identifying where it is?
A hex editor might be easiest.
You could also use Python:
print open("filewithnuls").read().replace("\0", ">>>NUL<<<")
Dustin
Hmm, interesting if I run:
print open("test.csv").read().replace("\0", ">>>NUL<<<")
every single character gets a >>>NUL<<< between them...
What the heck does that mean?
Example, here is the first field in the csv
89114608511,
the above code produces:
>>>NUL<<<8>>>NUL<<<9>>>NUL<<<1>>>NUL<<<1>>>NUL<<<4 >>>NUL<<<6>>>NUL<<<0>>>NUL<<<8>>>NUL<<<5>>>NUL<<<1 >>>NUL<<<1>>>NUL<<<,

I'm guessing that your file is in UTF-16, then -- Windows seems to do
that a lot.
Do what a lot? Encode data in UTF-16xE without putting in a BOM or
telling the world in some other fashion what x is? Humans seem to do
that occasionally. When they use Windows software, the result is
highly likely to be encoded in UTF-16LE -- unless of course the human
deliberately chooses otherwise (e.g. the "Unicode bigendian" option in
NotePad's "Save As" dialogue). Further, the data is likely to have a
BOM prepended.

The above is consistent with BOM-free UTF-16BE.

May 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
1
by: D A H | last post by:
I have gotten the same exception in multiple projects. I have solved the underlying problem. My question is if anyone knew of a setting that would cause this exception to be thrown. A...
2
by: Schorschi | last post by:
Can't seemd to get ReadFile API to work! Returns invalid handle error? =========================================================================== Ok, the visual basic gurus, help! The...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
1
by: Flack | last post by:
Hey guys, Here is whats happening. I have a StringBuilder, a TextBox, and a TabControl with one TabPage. On my main form, I created and displayed a fairly big maze. While the app is solving...
6
by: kikapu | last post by:
Hi to all, i have the following simple Socket Server code and a vb client that send to it some data using WinHttp. (very simple code, just open and send) The string is succesfully sent to the...
0
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
5
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.