Yup. I also just recieved permission from my supervisor to share the directory. Here is the 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')
tables = arcpy.ListFeatureClasses('*.shp')
print tables
tables.extend(arcpy.ListTables('*', 'dBASE'))
print tables
for table in tables:
print table
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(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='F:/FERC/raw_data/eqr_transaction_2009/EQRDATA'
DBFtoCSV(path)
Here is the error:
C:\>python dbf_convert_stack2.py
First print: []
Second print: [u'EQR_TRANSACTION_200903_AL.dbf', u'._EQR_TRANSACTION_200903_AL.dbf', u'EQR_TRA
NSACTION_200903_MZ.dbf', u'._EQR_TRANSACTION_200903_MZ.dbf', u'eqr_transaction_2
00903.dbf', u'._eqr_transaction_200903.dbf', u'EQR_TRANSACTION_200906_AL.dbf', u
'._EQR_TRANSACTION_200906_AL.dbf', u'EQR_TRANSACTION_200906_MZ.dbf', u'._EQR_TRA
NSACTION_200906_MZ.dbf', u'eqr_transaction_200906.dbf', u'._eqr_transaction_2009
06.dbf', u'eqr_transaction_200909_388.dbf', u'._eqr_transaction_200909_388.dbf',
u'EQR_TRANSACTION_200909_AL.dbf', u'._EQR_TRANSACTION_200909_AL.dbf', u'EQR_TRA
NSACTION_200909_MZ.dbf', u'._EQR_TRANSACTION_200909_MZ.dbf', u'eqr_transaction_2
00909.dbf', u'._eqr_transaction_200909.dbf', u'eqr_transaction_200912_388.dbf',
u'._eqr_transaction_200912_388.dbf', u'EQR_TRANSACTION_200912_AL.dbf', u'._EQR_T
RANSACTION_200912_AL.dbf', u'EQR_TRANSACTION_200912_MZ.dbf', u'._EQR_TRANSACTION
_200912_MZ.dbf', u'eqr_transaction_200912.dbf', u'._eqr_transaction_200912.dbf']
Third Print: EQR_TRANSACTION_200903_AL.dbf
Traceback (most recent call last):
File "dbf_convert_stack2.py", line 39, in <module>
DBFtoCSV(path)
File "dbf_convert_stack2.py", line 30, in DBFtoCSV
with arcpy.da.SearchCursor(table, fields) as cursor:
TypeError: 'field_names' must be string or non empty sequence of strings
My end goal is to import these into STATA, so CSV or Excel doesn't really matter. Just trying anything that may work. It's weird that I get a cannon open error with the Tableto Excel but not here.