Hi,

I'm getting this error when reading from a csv file: "Runtime Error!
line contains NULL byte".
Any idea about the root cause of this error?

If it helps, here's the part of my code where I'm reading the csv file.

def processFile():
global DATA # - Input file
global admin_id # - Comma separated list of admin IDs
global vf # - Verified list
global nf # - Non-verified list
global remainingAdmins # - Higher level admin IDs contained in the file,
# but not specified in the admin_id list that was used to generate input file
global counter # - Counter indicating row number


counter = 1
fieldCounter = 0

DATA = open(optDict.get('--DATA'), 'r')

vf = []
nf = []
remainingAdmins = ''
country = ''

for i in range(5):
vf.append('')
nf.append('')

oldline = None
currInd = 0
prevInd = -1

try:
reader = csv.reader(DATA)
for row in reader:

#counter = 1 --> first line in the reader
# row[0] = $A$1 cell - the comma separated list of Admin Area IDs
if counter == 1:
admin_id = row[0][row[0].index(':') +1:]

if counter >=3:
if len(row) == 13:
currInd = 0
flag = True

while flag:
if row[currInd] == '':
currInd += 2
else:
flag = False

if currInd == 0:
country = row[currInd]

if currInd <= prevInd:
fieldCounter += processFC(oldline, prevInd)
else:
if oldline != None:
fieldCounter += validateLine(oldline)
remainingAdmins += oldline[prevInd+1] + ','

prevInd = currInd
oldline = row

else:
log(["\n[Line# %d]: Invalid Record Syntax (Row Length expected 13 but was %d)\t" %(counter, len(row))])
log(["Row: %s\n" %row])
fieldCounter += 1

counter = counter +1

# process the last line
fieldCounter += processFC(oldline, prevInd)
remainingAdmins = remainingAdmins[:-1]

stmt = "\nTotal number of invalid field values: %d\n" %fieldCounter
log([stmt])
print stmt

finally:
if fieldCounter == 0:
stmt = "\nDATA Syntax Valid\n"
print stmt
log([stmt])
else:
raise Exception("Data file contains incorrect values")

Thanks,

Ok, I got it and thought I'd post the solution.
Simply yet caused me grief...
Used file was saved in a .xls format instead of a .csv
Didn't catch this because the file name itself had the .csv extension while the type was still .xls

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.