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?
TypeError: 'field_names' must be string or non empty sequence of strings
where is this variable coming from? is it in csv writer?
Would be good to include a print of the variable "fields" just after it is created (around line 21 for example) and before the cursor starts (around line 28) to see what's in it.
Thanks Xander. When I do that it just prints an empty list. Nothing never gets added to it. Something must be going wrong with ListFields
At both positions in the code?
Yeah. empty list in each case.
Must be a data issue. Are you sure you can't share a single DBF file to perform a test? If not register the issue at Esri and ask them to help you. Without access to the data there is little I can do.
I actually confirmed with my supervisor. I was wrong, the data is publicly available. could you friend/whatever needs to be done so I can send you a private message.
I just downloaded the DBF file (1.1 GB) and performed a test with Python and I get the same error. When I try to add the DBF to ArcMap the following error is displayed:
and when I try to open it with Excel it trows this (sorry for the Dutch interface):
Basically indicating that the file does not have the format of a DBF file or is damaged. Have you been able to open the file with any software? Or can you convert the "DBF" to a different format?
Oh okay. The only software I have been able to open it in is OpenOffice. The problem is that OpenOffice cuts off extra rows as these files exceed its max capacity. This isn't a deal breaker but would like to have the full data. Using OpenOffice, I could convert them into csv manually
Loosing data isn't the ideal scenario. What software was used to create the DBF files?
One thing we solved is that it wasn't the python code, but ArcGIS simply isn't able to open the file. That is the reason for getting a blank list of field names.