Hi Everyone, I am trying to convert dbf to csv using the following code:
import arcpy import os import csv def DBFtoCSV(path): '''Convert every DBF table into CSV table. ''' arcpy.env.workspace = path tablelist=arcpy.ListTables('*', 'dBASE') for table in tablelist: outputFile = '{}.csv'.format(table.split('.dbf')[0]) # Get the fields in the dbf to use for the cursor and csv header row. fields = [] for field in arcpy.ListFields(table): fields.append(str(field.name)) # Make the csv. with open((os.path.join(path,outputFile)),'wb') as output: dataWriter = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) # Write header row. dataWriter.writerow(fields) # Write each row of data to the csv. with arcpy.da.SearchCursor(table, fields) as cursor: for row in cursor: dataWriter.writerow(row) print('Finished creating {}'.format(outputFile)) if __name__ == '__main__': path=r'F:\DataLocation' DBFtoCSV(path)
I am getting an error at the SearchCursor because it says field names must be string or non empty. I added the print statement and it turns out my list of fields is empty. Can anyone tell me why this is?
I haven't yet. Will look into that now. If I cannot open them in ArcGIS, does that mean I cannot use arcpy tools to convert?
dbase files can be added to arcmap no problem
Ok, once I get into arcmap, I will get back to you on what it looks like.
Will there be any sign to look for which would tell me why the listfields is not working?
just write a small script to read it directly from disk in a simple location using listtables, then list fields using the syntax in the link I sent.
import arcpy # For each field in the Hospitals feature class, print # the field name, type, and length.
fields = arcpy.ListFields("c:/test/myfile.dbf")
for field in fields:
print("{0} is a type of {1} with a length of {2}" .format(field.name, field.type, field.length))
It's strange. Even when I feed in the direct location of the file, list fields is still returning an empty list?
I made a separate script just to test.
import dbf import arcpy import os import csv path=r'F:/Datalocation/' arcpy.env.workspace = path fields=arcpy.ListFields(r'F:Datalocation//dataset.dbf') print fields
Returns an empty list.
When I run the exact code you give me. Nothing prints. The listfields list continues to be empty so there is nothing to iterate over. I am not sure why.
you are mixing filepath syntaxes
r'F:\Datalocation\dataset.dbf'
or
'F:/Datalocation/dataset.dbf'
Thanks Dan. Just Fixed that but still having same problem.
import arcpy import os import csv path='F:/Datalocation/' arcpy.env.workspace = path fields=arcpy.ListFields('F:/datalocation/dataname.dbf') for field in fields: print("{0} is a type of {1} with a length of {2}" .format(field.name, field.type, field.length)) #This doesn't print. Fields is empty fieldlist1=[] tablelist = arcpy.ListTables() # list tables in file for table in tablelist: fieldlist1.append(arcpy.ListFields(table)) print fieldlist1 #This part gives me an "ERROR 999999: ERROR EXECUTING FUNCTION"
attach the dbf... you may have to zip it to get past upload file types.
Thanks Dan. Unfortunately, this is confidential data for a research project. I will look more into it but I can't share it. Is there a way to check if the file is corrupted which could be explaining the error?
open it up in a spreadsheet or some other program
export it out to a csv
if arcmap can load it and you can see the table in arcmap then it should be fine... if not, then it isn't