I fixed the Null byte problem by using DictReader and DictWriter, but now I am running into a NoneType error when the new spreadsheet is created. The spreadsheet does have empty cells, could this be causing the problem? If so, what's the resolution to this?
import arcpy,collections,re,csv
arcpy.env.overwriteOutput = True
workspace= # path to workspace
file = # path to csv to be read
outfile=# path to csv to be written
input= # path to reference feature class
lstflds=arcpy.ListFields(input)
with open(file, 'rb') as csvfile:
with open(outfile,'wb') as f:
field_names=['Rule','Attribute','Operator','Value','ERROR1',
'ERROR2','FIXED','Feature Class']
reader = csv.DictReader(csvfile,field_names,delimiter=',')
writer=csv.DictWriter(f,field_names)
writer.writeheader()
for row in reader:
mydict = collections.defaultdict(list)
domains=arcpy.da.ListDomains(workspace)
for domain in domains:
if domain.type =='Text':
coded_values=domain.codedValues
for code,desc in coded_values.iteritems():
mydict[domain.name].append(code)
dict_dom={}
for fld in lstflds:
for dom,values in mydict.iteritems():
if fld.domain==str(dom):
dict_dom.update({fld.name:values})
for keys,vals in dict_dom.iteritems():
if keys == row['Attribute']:
char_removal = ["[","'","u"," ","]"]
rx = '[' + re.escape(''.join(char_removal)) + ']'
v=re.sub(rx,'', str(vals))
row['Value']= v.replace('\x00','')
writer.writerow(row)
The error:
self.writer.writerow(self._dict_to_list(rowdict))
File "C:\Python27\ArcGIS10.1\lib\csv.py", line 144, in _dict_to_list
", ".join(wrong_fields))
TypeError: sequence item 0: expected string, NoneType found