473,698 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

_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 11372
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...@we bsafe.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.Ele mentTree 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(myfi le)

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_heartbe at"] = heartbeat
mainbox.attrib["sw_ver"] = sw_ver
mainbox.attrib["hw_ver"] = hw_ver
mainbox.attrib["date_activated "] = activated
mainbox.attrib["mac_addres s"] = mac
mainbox.attrib["boxid"] = boxid

indent(root)

ElementTree(roo t).write('test. xml', encoding='UTF-8')

The traceback is as follows:

Traceback (most recent call last):
File "createXMLPacka ge.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************ **********@o5g2 000hsb.googlegr oups.com>, fscked wrote:
The traceback is as follows:

Traceback (most recent call last):
File "createXMLPacka ge.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.net wrote:
In <1178209090.674 787.202...@o5g2 000hsb.googlegr oups.com>, fscked wrote:
The traceback is as follows:
Traceback (most recent call last):
File "createXMLPacka ge.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("filewithn uls").read().re place("\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("filewithn uls").read().re place("\0", ">>>NUL<<<" )

Dustin
Hmm, interesting if I run:

print open("test.csv" ).read().replac e("\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>>>NU L<<<9>>>NUL<<<1 >>>NUL<<<1>>>NU L<<<4>>>NUL<<<6 >>>NUL<<<0>>>NU L<<<8>>>NUL<<<5 >>>NUL<<<1>>>NU L<<<1>>>NUL<<< ,
May 3 '07 #7
On Thu, May 03, 2007 at 10:28:34AM -0700, IA********@gmai l.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("filewithn uls").read().re place("\0", ">>>NUL<<<" )

Dustin

Hmm, interesting if I run:

print open("test.csv" ).read().replac e("\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().rep lace("\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().rep lace("\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_en coding = "utf8"
efrom = codecs.lookup(f rom_encoding)
einter = codecs.lookup(i ntermediate_enc oding)
rstream = codecs.StreamRe coder(stream, einter.encode, efrom.decode,
efrom.streamrea der, einter.streamwr iter)

for row in csv.reader(rstr eam, *args, **kw):
yield [unicode(column, intermediate_en coding) for column in row]

def main():
file_encoding = "utf16"

# generate sample data:
data = u"\xe4hnlich,\x fcblich\r\nalph a,beta\r\ngamma ,delta\r\n"
with open("tmp.txt", "wb") as f:
f.write(data.en code(file_encod ing))

# 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...@gmai l.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("filewithn uls").read().re place("\0", ">>>NUL<<<" )
Dustin
Hmm, interesting if I run:
print open("test.csv" ).read().replac e("\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>>>NU L<<<9>>>NUL<<<1 >>>NUL<<<1>>>NU L<<<4>>>NUL<<<6 >>>NUL<<<0>>>NU L<<<8>>>NUL<<<5 >>>NUL<<<1>>>NU L<<<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
5032
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 double-checked the path to my error log. It is in /var/www/logs/php_error_log Thanks. :) -Wayne Stevenson
8
21910
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 that time, I get a SocketException with the message "Only one usage of each socket address (protocol/network address/port) is normally permitted". This happen if you use localhost or between two distinct computers. And, for a period of time after the...
1
1894
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 codeveloper on the same project can get a copy of the project from VSS and does not get this exception, ever. I do. We both have the code that causes this exception to be thrown. We do NOT checkin our
2
5729
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 following is a diskette class (vb .net) that works find, in that I can validate a diskette is mounted, dismount it, lock it, unlock it, get diskette geometry, etc., all with a valid handle from CreateFile API! I can even position the file pointer,...
0
3713
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) : base(message){} public FtpException(string message, Exception innerException) : base(message,innerException){}
1
8601
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 the maze, it appends each step it takes to the StringBuilder. When the maze is solved, the user can click a menu item to display the results. When the menu item is clicked, I set the TextBox Text value using the StringBuilder's ToString()...
6
1880
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 server but not the reverse when the server just replies with a string. The client reports (last dll error) that "the server returned an invalid or unrecognized response".
0
3484
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 datasource import java.awt.BorderLayout; import java.awt.Component; import java.io.*; import java.net.URL; import javax.media.*;
5
2322
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 explaining a "lamda" expression and so I don't know what one is and I am not doing any database stuff in the entire program. So what does this error message really mean and what can I do to get an On Error into the routine?
0
8601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8892
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8860
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7716
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.