I have been trying all day to convert dbf files to CSV and cannot seem to get it. I have looked at various options and cannot seem to get one that will work. Here is one that I have been trying.
import arcpy
import dbf
from arcpy import env
import os
def DBFtoCSV(path):
'''Convert every DBF table into CSV table.
'''
env.workspace = path
tablelist = arcpy.ListTables() # list tables in file
for table in tablelist: # iterate through every table
#make sure you are just working with .dbf tables
if table.endswith('.dbf'):
with dbf.Table(os.path.join(path, table)) as current_table:
print current_table
dbf.export(current_table)
print "\n Processing ",table[:-4]+".csv table complete."
if __name__ == '__main__':
path=r'path'
DBFtoCSV(path)
The error I am getting now is:
Processing name.csv table complete.
Table: F:/name.dbf
Type: Visual Foxpro
Codepage: cp1252 (Windows ANSI)
Status: read-write
Last updated: 2014-02-24
Record count: 4887170
Field count: 23
Record length: 235
--Fields--
0) respondent I binary
1) report_yr I binary
2) report_prd I binary
3) sys_key I binary
4) tr_id C(24)
5) tr_contrac I binary null
6) tr_begin_d T binary null
7) tr_end_dat T binary null
8) tr_timezon C(2) null
9) tr_delv_ct C(4) null
10) tr_delv_sp C(48) null
11) tr_class_n C(4) null
12) tr_term_na C(4) null
13) tr_inc_nam C(4) null
14) tr_inc_pea C(4) null
15) tr_prod_na C(49) null
16) tr_quantit B binary null
17) tr_price B binary
18) tr_units C(9) null
19) tr_tot_tra B binary null
20) tr_tot_tr2 B binary null
21) tr_other M
22) tr_revised T binary
array('c', '\x00\x00')
16
(2, 0)
(235, array('c', ' \x8f\x04\x00\x00\xd9\x07\x00\x00\x03\x00\x00\x00\x01\x00\x00\
x001Q09 \x04\x00\x00\x001u%\x00\xe5\x03\x00\x00\x8au%\x00\x18
X&\x05MPPNM PNM Switchyard F LT M FP CAPA
CITY \x00\x00\x00\x00\x80+\x18A\xba\xda\
x8a\xfdew\x0f@$/KW-MO \x00\x00\x00\x00\x00\x00\x00\x00\xcd\xcc\xcc\xccR\xc47A\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'))
('0', 233, 2, 235, 0, 5, <function none at 0x110DF9B0>, <function none at 0x110D
F9B0>)
array('c', '\x00\x00')
Traceback (most recent call last):
File "dbf_convert_stack.py", line 20, in <module>
DBFtoCSV(path)
File "dbf_convert_stack.py", line 16, in DBFtoCSV
dbf.export(current_table)
File "C:\Python27\ArcGIS10.4\lib\site-packages\dbf\ver_2.py", line 7859, in ex
port
data = record[fieldname]
File "C:\Python27\ArcGIS10.4\lib\site-packages\dbf\ver_2.py", line 2541, in __
getitem__
return self.__getattr__(item)
File "C:\Python27\ArcGIS10.4\lib\site-packages\dbf\ver_2.py", line 2508, in __
getattr__
value = self._retrieve_field_value(index, name)
File "C:\Python27\ArcGIS10.4\lib\site-packages\dbf\ver_2.py", line 2693, in _r
etrieve_field_value
if ord(null_data[byte]) >> bit & 1:
IndexError: array index out of range