-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbfs2csv-py2.py
48 lines (44 loc) · 1.93 KB
/
dbfs2csv-py2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import fnmatch
import os
import csv
import time
import datetime
import sys
from dbfread import DBF, FieldParser, InvalidValue # pip install dbfread
class MyFieldParser(FieldParser):
def parse(self, field, data):
try:
return FieldParser.parse(self, field, data)
except ValueError:
return InvalidValue(data)
debugmode=0 # Set to 1 to catch all the errors.
for infile in os.listdir('.'):
if fnmatch.fnmatch(infile, '*.dbf'):
outfile = infile[:-4] + ".csv"
print("Converting " + infile + " to " + outfile + ". Each period represents 2,000 records.")
counter = 0
starttime=time.clock()
with open(outfile, 'wb') as csvfile:
table = DBF(infile, parserclass=MyFieldParser)
writer = csv.writer(csvfile)
writer.writerow(table.field_names)
for i, record in enumerate(table):
for name, value in record.items():
if isinstance(value, InvalidValue):
if debugmode == 1:
print('records[{}][{!r}] == {!r}'.format(i, name, value))
writer.writerow(list(record.values()))
counter +=1
if counter%100000==0:
sys.stdout.write('!' + '\r\n')
endtime=time.clock()
print str("{:,}".format(counter)) + " records in " + str(endtime-starttime) + " seconds."
elif counter%2000==0:
sys.stdout.write('.')
else:
pass
print("")
endtime=time.clock()
print "Processed " + str("{:,}".format(counter)) + " records in " + str(endtime-starttime) + " seconds (" + str((endtime-starttime)/60) + " minutes.)"
print str(counter / (endtime-starttime)) + " records per second."
print("")